how can I make a tkinter variable set as random int change when iterating through for loop

130 views Asked by At

I'm learning python and attempting to make a die rolling app. It is mostly functional, but this little bit of code isn't functioning right.

The goal of the program is to take a user input for the number of dice to roll and the number of faces on the dice, and output random numbers for each die, within the range(number of faces)

def die_count(*args):
    x = 0
    count = int(total.get())
    faces = int(sides.get())
    for i in range(count):
        die_value.set(random.randint(1, faces))
        ttk.Label(frame, textvariable = die_value).grid(row = 3, column = x)
        x = x + 1

I'm just learning tkinter as well, so possibly there is some arcane behavior of .set() that I have yet to learn... I would expect this code to iterate through the loop count times, reassigning die_value every time. It loops correctly, but the die_value remains the same in each label.

an example output for the inputs total = 3 and faces = 6, would be 5 5 5, and on a rerun, 3 3 3, or any other numbers in the range repeated "total" number of times.

The range for the outputs is correct, in that it never exceeds the bounds of the value input for sides. The individual values though, are supposed to be different from one another, and I'm not sure whats causing them not to be.

below is the entire code (I know its a hot mess, but I want to get it working, then make it pretty)

import random
from tkinter import *
from tkinter import ttk

window = Tk()
window.title("die roll")
window.geometry("500x500")


def die_count(*args):
    x = 0
    count = int(total.get())
    faces = int(sides.get())
    for i in range(count):
        die_value.set(random.randint(1, faces))
        ttk.Label(frame, textvariable = die_value).grid(row = 3, column = x)
        x = x + 1

die_value = IntVar()
total = IntVar()
sides = IntVar()

ttk.Label(window, text='how many dice would \n you like to roll?',borderwidth=1).grid(row=0,column=0)

ttk.Entry(window,textvariable=total).grid(row=0,column=1)

ttk.Label(window, text='how many sides \n should the dice have?',borderwidth=1).grid(row=1,column=0)

ttk.Entry(window, textvariable=sides).grid(row=1,column=1)

ttk.Button(window, text="Roll", command = die_count).grid(row = 2,column=0)

frame = ttk.Frame(window).grid(row=2, column=1)

window.mainloop()
2

There are 2 answers

1
George Ogden On BEST ANSWER

Here is the code:

def die_count(*args):
    x = 0
    count = int(total.get())
    faces = int(sides.get())
    for i in range(count):
        ttk.Label(frame, text = random.randint(1, faces)).grid(row = 3, column = x)
        x += 1

I have changed textvariable to text, because it would "bind" the textvariable, so every time you changed the textvariable, it would change the value (which is why they all ended up with the value of the last roll). With text, this does not occur.

3
quamrana On

You seem to only have one instance of die_value. I assume your Labels need one each:

    for i in range(count):
        die_value = IntVar()
        die_value.set(random.randint(1, faces))