Python Tkinter 窗口输出和 Shell 输入

发布于 2025-01-06 14:20:22 字数 689 浏览 3 评论 0原文

我在 IDE 中的 python shell 和 Tkinter 窗口之间遇到问题。我想做的是将所有用户输入放入 shell 中,然后我想在 Tkinter 窗口中输出相应的信息。

但是,当我的窗口创建并弹出时,我将其关闭以在 shell 中继续我的程序,然后继续输入。但是,当我尝试重新初始化窗口时。据说窗户已被毁坏。我明白这意味着什么,所以我尝试有一个顶级窗口,在其中输出可以关闭的信息,并隐藏我的根窗口,但 shell 不会继续,直到我也关闭/销毁根窗口。

有没有办法可以在不破坏根窗口的情况下继续在 shell 中运行?我对这门语言相当陌生,因此非常感谢任何帮助。

这是我的总体想法:

from Tkinter import *

#get all my info from the shell

root = Tk()
root.withdraw()  #hide the root window

main = Toplevel()
     #this is the window that I want to be able to close and open later

#get more info from the shell after main is closed

#now I want to open the updated main window

提前致谢! (如果这很重要的话,我正在 Windows 上工作)

I am having a problem between the python shell in my IDE and the Tkinter window. What I am trying to do is have all of my user input in the shell, and then I would like to output the corresponding information in a Tkinter window.

However, when my window is made and pops up, I close it to continue my program in the shell, then I continue with input. However, when I try to reinitialize my window. It says that the window has been destroyed. I understand what this means so I tried having a Toplevel window where I output my info which can be closed, and hide my root window, but the shell will not continue until I close/destroy the root window as well.

Is there a way I can continue in the shell without destroying my root window? I am fairly new to this language so any help would be very much appreciated.

This is my general idea:

from Tkinter import *

#get all my info from the shell

root = Tk()
root.withdraw()  #hide the root window

main = Toplevel()
     #this is the window that I want to be able to close and open later

#get more info from the shell after main is closed

#now I want to open the updated main window

Thanks in advance! (And I am working on Windows if that matters)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剩一世无双 2025-01-13 14:20:22

我不确定您尝试执行此操作的方式是否是最有效的方法,但到目前为止我会提出这些更改:

from Tkinter import *

#get all my info from the shell

window = Tk()
window.iconify()  #hide the root window

#get more info from the shell after main is closed

window.deiconify()
window.mainloop() # to handle events

我重命名了您的根窗口,以便您更清楚地了解发生了什么,并删除了多余的(恕我直言) )额外的顶级窗口!

另请记住,如果没有主循环和必要的事件处理程序,您将无法完成任何事情!

I'm not sure if the way you are trying to do this is the most efficient way, but i would propose these changes so far:

from Tkinter import *

#get all my info from the shell

window = Tk()
window.iconify()  #hide the root window

#get more info from the shell after main is closed

window.deiconify()
window.mainloop() # to handle events

i renamed your root-Window to make it more clear for you whats happening and removed the superflous (imho) additional Toplevel-window!

Also keep in mind, that you won't accomplish anything without the mainloop and the necessary event-handlers!

秋意浓 2025-01-13 14:20:22

简而言之,这不是 Tkinter 的设计工作方式。 Tkinter 被设计为具有创建一次的单个根窗口,以及正在运行的单个事件循环。以任何其他方式使用它必然会导致不良行为。

如果您确实需要代码以这种方式工作,请在一个进程中从 shell 收集输入,然后使用单独的进程来显示 tkinter 窗口。您可以使用套接字相互通信,也可以通过参数、环境变量或临时文件将数据从父级传递给子级。

Simply put, this is not how Tkinter is designed to work. Tkinter was designed to have a single root window that is created once, and with a single eventloop that is running. Using it any other way is bound to lead to undesired behavior.

If you really need code to work this way, gather your input from your shell in one process, then use a separate process to display the tkinter window. You can either communicate from one the other using a socket, or you could pass the data from the parent to the child via arguments or environment variables or temporary files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文