【问题标题】:Python 3, tkinter, widgets not showing upPython 3,tkinter,小部件未显示
【发布时间】:2019-03-15 02:18:14
【问题描述】:

我在 Windows 10 中使用 Python 3,tkinter 模块。我知道这个问题经常被问到,但是这个论坛上的问题(或谷歌的任何其他结果)似乎都没有答案。我尝试开始使用 tkinter(我以前从未使用过),但每个真正基本的工作示例只返回主 tkinter GUI 窗口,没有小部件。我也尝试运行这个论坛上提出的问题的大多数解决方案(如Python tkinter widgets not showingTkinter widgets not appearingTkinter widgets not showing),但结果相同(只是主窗口,没有调整主窗口或任何小部件都在显示)。

我目前的 MWE 是:

from tkinter import *
from tkinter import messagebox

top = Tk()
top.geometry("100x100")

def helloCallBack():
   msg = messagebox.showinfo( "Hello Python", "Hello World")

B = Button(top, text = "Hello", command = helloCallBack)
B.place(x = 50,y = 50)

top.mainloop()

I am following this tutorial。没有错误出现。
结果见附图:

我通过Notepad++在cmd中运行代码,这对于所有不涉及tkinter的普通python代码都可以正常工作。

【问题讨论】:

  • 我在 ipython 中运行了你的代码
  • 而且它在python IDLE中也有效。
  • 代码也可以在 linux 的控制台上运行。
  • 感谢大家的检查,您知道如何使它适用于 Notepad++ 吗?您需要更多信息吗?
  • 我注意到按钮似乎在窗口边缘关闭...所以我注释掉了窗口几何线,您是否尝试调整窗口大小或只是跳过几何线?再想一想。

标签: python python-3.x tkinter notepad++


【解决方案1】:

试试这个:

B = Button(top, text = "Hello", command = helloCallBack).pack(top)

代替:

B = Button(top, text = "Hello", command = helloCallBack)
B.place(x = 50,y = 50)

Place 可用于设置您希望内容在窗口中出现的位置。

【讨论】:

  • 虽然此代码可能会解决问题,including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
猜你喜欢
  • 2011-08-21
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 2021-05-29
  • 2019-04-23
  • 1970-01-01
  • 2018-07-15
  • 1970-01-01
相关资源
最近更新 更多