在Python中使用Tkinter多个框架

1 投票
1 回答
3695 浏览
提问于 2025-04-16 07:03

你好,我一直在尝试制作一个简单的图形界面(GUI),这个界面可以通过一个输入框来接受字符串,这个输入框是Tkinter模块里的。根据字符串所在的容器,它要么通过终端运行“killall + 字符串”,要么运行“open + 字符串”。下面是我正在使用的代码。问题出在第8行,前面的注释详细解释了哪里出了问题。

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()

我认为问题出在多个框架(frames)之间,因为之前只有form1作为form_and_submit类的实例时,它运行得很好。感谢任何能帮助解决这个问题的人。

1 个回答

0

这可能不是你想要的答案,但我可以通过去掉这个类来让它正常工作。以下是我的代码:

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()

撰写回答