Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter的第二个模块中未显示提交按钮_Python_Tkinter - Fatal编程技术网

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

Python Tkinter的第二个模块中未显示提交按钮,python,tkinter,Python,Tkinter,我有两个模块file1.py和file2.py。在file1.py中,我创建了一个类和一个带有标签和条目小部件的函数。在file2.py中,我继承了file1.py类,并在函数中创建了一个提交按钮。因此,当我单击submit按钮时,应该显示在file1.py的entry小部件中输入的值。但我观察到的是,提交按钮没有显示,当我关闭窗口时,输入的值会显示出来。我无法理解这种行为,有人能纠正我的错误吗 file1.py file2.py 我明白你的问题所在,要想让它发挥作用,你必须解决一些问题: 首先

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

file1.py file2.py
我明白你的问题所在,要想让它发挥作用,你必须解决一些问题:

首先,您需要使用导入的类作为
app
,该类具有提交按钮,以便仍然能够仅运行file1。您可以在file1.py中检查
\uuu name\uuuuuuuuu
是否为
'\uuuuu main\uuuuuuuuuu'
,如:

if __name__ == '__main__':
    app = TestClass(master = top)
    top.minsize(400, 400)
    top.mainloop()
其次,您的函数没有被调用,因为您调用了该函数并将结果提供给
按钮
,在这里您应该只传递函数而不调用它:

self.s = Button(self.frame, text="Submit", command=get_func())
在函数本身中,不应使用全局变量,如
app
,因为,例如,如果您有同一类的多个实例,它们都将依赖于一个实例,并且在
TestClass
中,您已经设置了
self.x
,这也可以在
ImportClass
中访问,因此您应该将
print
语句替换为
print self.x.get()
而不是
print app.x.get()
将主程序从
ImportClass
设置为
top
。我还添加了
*args
**kwargs
以在
\uuuuu init\uuuu
方法中传递,因此您可以得到:

file1.py 和file2.py
所以这应该行得通。希望这能帮助您避免类似的进一步问题。

您遇到问题的主要原因是,在
Tk
实例关闭或发生事件之前,
mainloop
之后不会运行任何行。当您
import file1
时,
mainloop
最终运行,然后等待GUI关闭,以便首先定义
ImportClass
,然后为其初始化对象

只需删除:

top.mainloop()
文件1
中添加:

top.mainloop()
file2
作为最后一行


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

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


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

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()
top.mainloop()
top.mainloop()
self.s = Button(..., command=get_func())
self.s = Button(..., command=get_func)