从 python 中的文件调用另一个 GUI
Calling another GUI from a file in python
假设我有一个简单代码如下的GUI
它上面有一个按钮,单击它时我想弹出另一个 GUI,然后从中调用该函数。问题是当我 运行 第一个文件时,另一个文件的 GUI 自动弹出。我该怎么办
first
文件的代码如下
from tkinter import *
import another
root = Tk()
button1 = Button(root, text = "Call" , command = another.abc)
button1.pack()
root.mainloop()
第二个文件another.py
代码如下
from tkinter import *
root_Test2 = Tk()
root_Test2.geometry('450x450')
def abc():
print("that's working")
root_Test2.mainloop()
请提供帮助我在单击第一个按钮时打开第二个 window 的解决方案
根据@PM 2Ring,您可以将第二个文件的代码更改为:
from tkinter import *
if __name__ == '__main__':
root_Test2 = Tk()
root_Test2.geometry('450x450')
def abc():
print("that's working")
if __name__ == '__main__':
root_Test2.mainloop()
您可以找到有关 if __name__ == '__main__'
here
的更多信息
假设我有一个简单代码如下的GUI 它上面有一个按钮,单击它时我想弹出另一个 GUI,然后从中调用该函数。问题是当我 运行 第一个文件时,另一个文件的 GUI 自动弹出。我该怎么办
first
文件的代码如下
from tkinter import *
import another
root = Tk()
button1 = Button(root, text = "Call" , command = another.abc)
button1.pack()
root.mainloop()
第二个文件another.py
代码如下
from tkinter import *
root_Test2 = Tk()
root_Test2.geometry('450x450')
def abc():
print("that's working")
root_Test2.mainloop()
请提供帮助我在单击第一个按钮时打开第二个 window 的解决方案
根据@PM 2Ring,您可以将第二个文件的代码更改为:
from tkinter import *
if __name__ == '__main__':
root_Test2 = Tk()
root_Test2.geometry('450x450')
def abc():
print("that's working")
if __name__ == '__main__':
root_Test2.mainloop()
您可以找到有关 if __name__ == '__main__'
here