简体   繁体   English

如何在遍历 for 循环时将 tkinter 变量设置为随机 int 变化

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

I'm learning python and attempting to make a die rolling app.我正在学习 python 并尝试制作一个滚动应用程序。 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)该程序的目标是在范围(面数)范围内接受用户输入的骰子数和骰子上的面数,以及每个骰子的 output 随机数

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.我也只是在学习 tkinter,所以可能存在一些我尚未学习的.set()神秘行为......我希望这段代码能够遍历循环计数时间,每次都重新分配die_value It loops correctly, but the die_value remains the same in each label.它循环正确,但每个 label 中的 die_value 保持不变。

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.一个示例 output 对于输入总 = 3 和面 = 6,将是 5 5 5,并且在重新运行时,3 3 3 或范围内重复“总”次数的任何其他数字。

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

You seem to only have one instance of die_value .您似乎只有一个die_value实例。 I assume your Labels need one each:我假设您的标签每个都需要一个:

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

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).我已将 textvariable 更改为 text,因为它会“绑定” textvariable ,因此每次更改textvariable时,它都会更改值(这就是为什么它们都以最后一卷的值结束)。 With text , this does not occur.使用text ,这不会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM