英文:
Why is tkinter not displaying anything?
问题
我正在尝试使用tkinter制作一个UI游戏,但在打开的窗口中没有显示任何内容,在shell中也没有错误。
```python
import tkinter as tk
count = 0
def on_button_click():
global count
count += 1
label.config(text="You have clicked " + str(count) + " times!")
root = tk.Tk()
root.title("My Game")
root.geometry("800x600")
# 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()
我尝试过搜索不同的tkinter教程,但它们都表现得一样,是否有明显的遗漏。
<details>
<summary>英文:</summary>
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()
[The output of the script ](https://i.stack.imgur.com/8PFd7.png)
i have tried searching up different tutorials on tkinter but they all act the same, is it something obvious I'm missing
</details>
# 答案1
**得分**: 3
你的标签和按钮创建代码位于root.mainloop()之外,并且你有两次mainloop()行。
<details>
<summary>英文:</summary>
Your label & button creation code is outside your root.mainloop() and you have the mainloop() line twice.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论