0

In this case, the button presses create the number that is needed to be displayed in the GUI. I am trying to make a dice roller where everything is handled through a Tkinter GUI, and outside of the shell. As of right now the output of the button presses is displayed in the console rather than in the label created. I have an incomplete label included as a blank space to include the solution once I have it.

Thank you for your help in advance!

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()
Micah
  • 13
  • 1

1 Answers1

1

I've completely reworked your code. It now displays the result in the window, and the code is far more efficient.

Here it is:

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

Hope this helps!

10 Rep
  • 2,217
  • 7
  • 19
  • 33
  • Will be accepting this answer as soon as the 5 minutes are up. Thank you very much! I see exactly what I was missing with this. – Micah May 08 '20 at 18:19
  • @Micah What 5 minutes? And thanks, I am glad to help! – 10 Rep May 08 '20 at 18:20
  • It puts a 5 minute delay before I am allowed to accept the answer. No idea if it has to do with this being my first time asking a question on a brand new account or if it's something else. – Micah May 08 '20 at 18:23
  • @Micah Ok THanks! – 10 Rep May 08 '20 at 18:24
  • Users with less than 15 reputation do not have their votes shown but they are recorded according to the pop-up when I try to upvote. – Micah May 08 '20 at 18:28
  • @Micah Ok then. No prob1 – 10 Rep May 08 '20 at 18:29