Как распечатать результат нажатия кнопки на этикетке? - PullRequest
0 голосов
/ 08 мая 2020

В этом случае нажатие кнопки создает число, которое необходимо отобразить в 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()

1 Ответ

0 голосов
/ 08 мая 2020

Я полностью переработал ваш код. Теперь он отображает результат в окне, и код намного эффективнее.

Вот он:

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()

Надеюсь, это поможет!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...