【发布时间】:2020-08-24 09:00:32
【问题描述】:
在这种情况下,按下按钮会创建需要在 GUI 中显示的数字。我正在尝试制作一个骰子滚轮,其中所有内容都通过 Tkinter GUI 在外壳之外进行处理。截至目前,按钮按下的输出显示在控制台中,而不是创建的标签中。我有一个不完整的标签作为空格包含在我拥有解决方案后。
提前感谢您的帮助!
import tkinter as tk
root = tk.Tk()
root.title('Choose the dice to roll.')
frame = tk.Frame(root)
frame.pack()
def d20():
import random
for x in range(1):
print (random.randint(1,20))
def d12():
import random
for x in range(1):
print (random.randint(1,12))
def d8():
import random
for x in range(1):
print (random.randint(1,8))
def d6():
import random
for x in range(1):
print (random.randint(1,6))
def d4():
import random
for x in range(1):
print (random.randint(1,4))
d20_button = tk.Button(frame,
text="Click here to roll a D20",
command=d20)
d20_button.pack(side=tk.LEFT)
d20_button.config(height = 10, width = 20)
d12_button = tk.Button(frame,
text="Click here to roll a D12",
command=d12)
d12_button.pack(side=tk.LEFT)
d12_button.config(height = 10, width = 20)
d8_button = tk.Button(frame,
text="Click here to roll a D8",
command=d8)
d8_button.pack(side=tk.LEFT)
d8_button.config(height = 10, width = 20)
d6_button = tk.Button(frame,
text="Click here to roll a D6",
command=d6)
d6_button.pack(side=tk.LEFT)
d6_button.config(height = 10, width = 20)
d4_button = tk.Button(frame,
text="Click here to roll a D4",
command=d4)
d4_button.pack(side=tk.LEFT)
d4_button.config(height = 10, width = 20)
quit_button = tk.Button(frame,
text="QUIT",
fg="red",
command=quit)
quit_button.pack(side=tk.LEFT)
quit_button.config(height =10, width = 5)
number_result = tk.Label(frame,
w.pack()
root.update()
root.mainloop()
【问题讨论】:
-
查看StringVars。
-
这能回答你的问题吗? making-python-tkinter-label-widget-update