【问题标题】:Multiple frames in Tkinter while using Python使用 Python 时 Tkinter 中的多个帧
【发布时间】:2024-01-23 16:51:02
【问题描述】:

嗨,我一直在尝试让一个简单的 GUI 工作,它使用 Tkinter 模块中包含的输入表单接受字符串,并且根据它所在的容器,运行“killall + string”或“open +字符串”通过终端。下面是我正在使用的代码。问题出在第 8 行,上面的 cmets 更详细地解释了问题所在。

from tkinter import *
import os
root = Tk()
def buttonPressed():
    # this is the problem in the program.
    # it keeps returning [''] in the print statement and doesn't quit
    # any applications
    listOfApps = form1.form.get().split(',')
    # the print is just so i can see the output
    print(listOfApps)
    # goes through each item in list and kills them(yes i know there are much easier
    # ways to do this, i'm just trying to learn a bit about GUI and the os module
    for i in listOfApps:
        try:
            os.system("killall " + i)
        except:
            pass
def buttonPressed2():
    filesToOpen = form2.form.get().split(', ')
    for i in filesToOpen:
        try:
            os.system("open " + i)
        except:
            pass
class form_and_submit:
    def _init_(self):
        pass
    #creates an instance of a form with seperate variables for button text, buton command, and color of the frame
    def create_container(self, buttonText, buttonCommand, color):
        #Creating all widgets/containers
        self.container = Frame(root)
        self.form = Entry(self.container)
        self.button = Button(self.container)
        #defining variables for all widgets/containers
        root['background'] = color
        self.container['background'] = color
        self.button['text'] = buttonText
        self.button['command'] = buttonCommand
        self.form['background'] = color
        self.form['border'] = '5'
        self.form['highlightthickness'] = '0'
        #Packing all widgets/containers
        self.container.pack()
        self.form.pack()
        self.button.pack()
#creating forms and putting them in root with desire attributes
form1 = form_and_submit
form2 = form_and_submit
form1.create_container(form1, 'kill matching processes', buttonPressed, 'red')
form2.create_container(form2, 'Open desired files', buttonPressed2, 'blue')
#starts up window
root.mainloop()

我相信这里的问题出在多个框架内,因为之前它只使用 form1 作为类 form_and_submit 的实例就可以正常工作。提前感谢任何可以帮助解决此问题的人。

【问题讨论】:

  • 1)。不要使用os.system。使用subprocess。 2)。它是__init__(两个下划线),而不是_init_
  • 感谢您的回答,难怪我无法检索类变量,因为我没有使用带有两个下划线的 init,这就是为什么我的类如此弄乱。稍微更改了我的代码,效果很好,再次感谢。

标签: python user-interface terminal python-3.x tkinter


【解决方案1】:

这可能不是您正在寻找的答案,但我可以通过摆脱课程来让它做正确的事情。这是我所拥有的:

from tkinter import *
import os
root = Tk()

def buttonPressed():
    # this is the problem in the program.
    # it keeps returning [''] in the print statement and doesn't quit
    # any applications
    listOfApps = form.get().split(',')
    # the print is just so i can see the output
    print(listOfApps)
    # goes through each item in list and kills them(yes i know there are much easier
    # ways to do this, i'm just trying to learn a bit about GUI and the os module
    for i in listOfApps:
        try:
            os.system("killall " + i)
        except:
            pass
def buttonPressed2():
    filesToOpen = form2.get().split(', ')
    for i in filesToOpen:
        try:
            os.system("open " + i)
        except:
            pass

container = Frame(root)
form = Entry(container)
button = Button(container)
root['background'] = 'red'
container['background'] = 'red'
button['text'] = 'kill matching processes'
button['command'] = buttonPressed
form['background'] = 'red'
form['border'] = '5'
form['highlightthickness'] = '0'


container2 = Frame(root)
form2 = Entry(container2)
button2 = Button(container2)
root['background'] = 'blue'
container2['background'] = 'blue'
button2['text'] = 'Open desired files'
button2['command'] = buttonPressed2
form2['background'] = 'blue'
form2['border'] = '5'
form2['highlightthickness'] = '0'

container.pack()
form.pack()
button.pack()

container2.pack()
form2.pack()
button2.pack()

#starts up window
root.mainloop()

【讨论】:

  • 感谢您的回答,但我使用类的原因是为了学习它的语法,我意识到没有类可以更轻松地完成,但这只是一个练习程序来帮助自己学习。