简体   繁体   English

从一个tkinter窗口打开另一个tkinter窗口

[英]Opening another tkinter window from one tkinter window

I have a Tkinter window in the file gui.py. 我在文件gui.py中有一个Tkinter窗口。 Upon the press of the Spacebar, I want to open another Tkinter window which is used to obtain an input from the user via the file imageinput.py. 按下空格键后,我想打开另一个Tkinter窗口,该窗口用于通过文件imageinput.py从用户获取输入。

So, I wrote the code to execute the run function of imageinput.py 因此,我编写了代码以执行imageinput.py的运行功能

def keyPressed(event, data):
    if event.keysym == "space": image_run()

When I run this, I get the following error: 运行此命令时,出现以下错误: 在此处输入图片说明

What is the best way to open such another Tkinter window this way? 这样打开另一个Tkinter窗口的最佳方法是什么?

Without knowing more of your code, you would create a new "top level" widget and use that widget like you used the original root top level window ( root = tkinter.Tk() ) as the parent of whatever widget hierarchy you create. 在不了解更多代码的情况下,您将创建一个新的“顶层”窗口小部件,并使用该窗口小部件,就像您使用原始的根顶层窗口( root = tkinter.Tk() )作为创建的任何窗口小部件层次结构的父级一样。 So... 所以...

def image_run(parent, *args, **kwargs):
    top = tkinter.Toplevel(parent)
    top.transient(parent)        
    canvas = tkinter.Canvas(top, ...)
    :
    :

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM