为什么 tkinter 没有显示任何内容?

huangapple go评论134阅读模式
英文:

Why is tkinter not displaying anything?

问题

  1. 我正在尝试使用tkinter制作一个UI游戏但在打开的窗口中没有显示任何内容shell中也没有错误
  2. ```python
  3. import tkinter as tk
  4. count = 0
  5. def on_button_click():
  6. global count
  7. count += 1
  8. label.config(text="You have clicked " + str(count) + " times!")
  9. root = tk.Tk()
  10. root.title("My Game")
  11. root.geometry("800x600")
  12. # button
  13. button = tk.Button(root, text="Click me", command=on_button_click)
  14. button.pack()
  15. # label
  16. label = tk.Label(root, text="Jitter click the button")
  17. label.pack()
  18. root.mainloop()

脚本的输出

我尝试过搜索不同的tkinter教程,但它们都表现得一样,是否有明显的遗漏。

  1. <details>
  2. <summary>英文:</summary>
  3. Im trying out tkinter to make a UI game but it is not displaying anything in the window that opens, there is no error in the shell.

import tkinter as tk

count = 0

def on_button_click():
global count
count += 1
label.config(text="You have clicked " + str(clicks) + " times!")

root = tk.Tk()
root.title("My Game")
root.geometry("800x600")

root.mainloop()

button

button = tk.Button(root, text ="Click me", command=on_button_click)
button.pack()

label

label = tk.Label(root, text="Jitter click the button")
label.pack()

root.mainloop()

  1. [The output of the script ](https://i.stack.imgur.com/8PFd7.png)
  2. i have tried searching up different tutorials on tkinter but they all act the same, is it something obvious I&#39;m missing
  3. </details>
  4. # 答案1
  5. **得分**: 3
  6. 你的标签和按钮创建代码位于root.mainloop()之外,并且你有两次mainloop()行。
  7. <details>
  8. <summary>英文:</summary>
  9. Your label &amp; button creation code is outside your root.mainloop() and you have the mainloop() line twice.
  10. </details>

huangapple
  • 本文由 发表于 2023年6月26日 03:39:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定