【问题标题】:How to print the output of a button press to a Label?如何将按钮按下的输出打印到标签?
【发布时间】: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()

【问题讨论】:

标签: python tkinter


【解决方案1】:

我已经彻底修改了您的代码。它现在在窗口中显示结果,并且代码效率更高。

这里是:

import tkinter as tk
import random
root = tk.Tk()
root.title('Choose the dice to roll.')
frame = tk.Frame(root)
frame.pack()

rand1 = random.randint(1,20)
rand2 = random.randint(1,12)
rand3 = random.randint(1,8)
rand4 = random.randint(1,6)
rand5 = random.randint(1,4)
number_result = tk.Label(frame, text = "")
number_result.pack()
def d20():
    for x in range(1):
        rand1 = random.randint(1,20)
        number_result.config(text = str(rand1))

def d12():

    for x in range(1):
        rand2 = random.randint(1,12)
        number_result.config(text = str(rand2))

def d8():

    for x in range(1):
        rand3 = random.randint(1,8)
        number_result.config(text = str(rand3))
def d6():

    for x in range(1):
        rand4 = random.randint(1,6)
        number_result.config(text = str(rand4))
def d4():

    for x in range(1):
        rand5 = random.randint(1,4)
        number_result.config(text = str(rand5))

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)


w.pack()
root.update()

root.mainloop()

希望这会有所帮助!

【讨论】:

  • 将在 5 分钟后立即接受此答案。非常感谢!我清楚地看到了我缺少的东西。
  • @Micah 什么 5 分钟?谢谢,我很乐意提供帮助!
  • 在我被允许接受答案之前会延迟 5 分钟。不知道这是否与我第一次在全新帐户上提出问题有关,或者是否是其他问题。
  • @Micah 好的,谢谢!
  • 声誉低于 15 的用户不会显示他们的投票,但当我尝试投票时,他们会根据弹出窗口记录。
猜你喜欢
  • 1970-01-01
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 2016-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多