提交按钮未显示在Tkinter的第二个模块中

时间:2018-02-10 08:55:30

标签: python tkinter

我有两个模块file1.py和file2.py。在file1.py中,我创建了一个类和一个带有标签和条目小部件的函数。在file2.py中,我继承了file1.py的类并在函数中创建了一个提交按钮。因此,当我单击提交按钮时,应显示在file1.py中的条目小部件中输入的值。但我观察到的是,提交按钮没有显示,当我关闭窗口时,显示输入的值。我无法理解这种行为,任何人都可以纠正我的错误。

file1.py

from Tkinter import *
top = Tk()


class TestClass(object):
    def __init__(self, master = None):
        self.frame = Frame(master)
        self.frame.pack()

        self.func()


    def func(self):
        self.label = Label(self.frame, text = "LabelName")
        self.label.pack()

        self.x = StringVar()
        self.entry = Entry(self.frame, textvariable=self.x)
        self.entry.pack()  

app = TestClass(master = top)
top.minsize(400, 400)
top.mainloop()

file2.py

from file1 import *

class ImportClass(TestClass):
    def __init__(self):
        super(ImportClass,self).__init__(master=None)
        self.imp_func()

    def imp_func(self): 
        def get_func():
            print app.x.get()

        self.s = Button(self.frame, text="Submit", command=get_func())
        self.s.pack()


Im = ImportClass()

2 个答案:

答案 0 :(得分:1)

我看到你的问题,为了让这个工作,你必须解决一些问题:

首先,您需要将导入的类app用作提交按钮,以便仍然只能运行file1,您可以在{。{}} __name__中检查{是{1}} '__main__' 1}}喜欢:

if __name__ == '__main__':
    app = TestClass(master = top)
    top.minsize(400, 400)
    top.mainloop()

其次,你的函数没有被调用,因为你调用函数并将结果提供给Button,在这里你应该只是传递函数而不调用它:

self.s = Button(self.frame, text="Submit", command=get_func())

在函数本身中你不应该使用像app这样的全局变量,因为例如如果你有同一个类的多个实例,它们都将依赖于一个实例并且在TestClass您设置了self.xImportClass也可以访问print,因此您应该将print self.x.get()语句替换为print app.x.get()而不是ImportClass来设置主人从top*args。我还添加了**kwargs__init__以便在from Tkinter import * class TestClass(object): def __init__(self, master = None): self.frame = Frame(master) self.frame.pack() self.func() def func(self): self.label = Label(self.frame, text = "LabelName") self.label.pack() self.x = StringVar() self.entry = Entry(self.frame, textvariable=self.x) self.entry.pack() if __name__ == '__main__': #just run if the file is called as main top = Tk() app = TestClass(master = top) top.minsize(400, 400) top.mainloop() 方法中传递,所以总而言之:

file1.py

from file1 import *
from Tkinter import *

class ImportClass(TestClass):
    def __init__(self, *args, **kwargs):
        #passing all args and kwargs to super
        super(ImportClass,self).__init__(*args, **kwargs)
        self.imp_func()

    def imp_func(self): 
        def get_func():
            print self.x.get()
            #using class property instead of global

        self.s = Button(self.frame, text="Submit", command=get_func)
        #pass function not it's return value which has been None
        self.s.pack()

if __name__ == '__main__':
    top = Tk()
    app = ImportClass(master = top)
    #using the ImportClass to display the window
    top.minsize(400, 400)
    top.mainloop()

和file2.py

 from bottle import route, run, request


@route('/')
def index():
    """ Display welcome & instruction messages """
    return "<p>Welcome to my extra simple bottle.py powered server!</p> \
           <p>The Web service can find a location from csv file \
           The way to invoke is :\
       <ul> \
              <li>http://localhost:8080/getlocation?postcode=xxxx</li>\
       </ul> \
       xxxx are the postcode you want to search."

@route('/getlocation/<postcode>')
def getlocation(postcode):
    csv_file = csv.reader(open('clinic_locations.csv', "r"), delimiter=",")
    #return{clinicname, latitude, longitude, email, state}
    for row in csv_file:
        if postcode == row[6]:
            return{row[3], row[8], row[9], row[7], row[5]}

run(host='localhost', port=8080, debug=True)

所以这应该有效。希望这有助于您防止出现此类问题。

答案 1 :(得分:1)

您遇到问题的主要原因是在mainloop之后没有行将运行,直到Tk实例关闭或事件发生。当您import file1mainloop最终运行,然后等待GUI关闭,以便首先定义ImportClass,然后再为其初始化对象。

只需删除:

top.mainloop()

来自file1并添加:

top.mainloop()

file2作为最后一行。

之后还有另一个问题,按钮的command选项需要引用可调用对象,而不是实际调用。替换:

self.s = Button(..., command=get_func())

使用:

self.s = Button(..., command=get_func)

另外,请注意我认为您的导入顺序相反,通过获取具有Tk实例的模块的GUI对象,反之亦然。