I am currently writing on a small hobby project and I have a problem concerning my list "dice" while using the dropdown menu it only ever shows the first iteration of the list (the single 0) but it is supposed to be updated in the dropdown menu after each press of the "roll the dice" button. How do I do that?
from random import randint
from tkinter import *
root = Tk()
root.title('Hobbyprojekt')
count = -1
global dice
dice = [0]
prpp= IntVar()
diceshow=Label()
#defining funtions for buttons
def roll():
global count
global diceshow
global dice
count +=1
print(count)
if count >= 1:
dice=[]
for x in range (0,7) :
dice.append(randint(1,10))
#calculating the viable dice options
for x in range (0,2) :
dice.remove(min(dice))
if count >= 1:
diceshow.destroy()
print("destroyed")
diceshow=Label(root, text=dice)
diceshow.grid(row=0,column=1)
print(dice)
print(dice[1])
print(dice[2])
print(dice[3])
#setting up the test gui
button1 = Button(root, text='Roll the dice', command=roll)
label1= Label(text='choice1')
label2= Label(text='choice2')
label3= Label(text='choice3')
label4= Label(text='choice4')
label5= Label(text='choice5')
label6= Label(text='choice6')
dd1= OptionMenu(root,prpp,*dice)
dd2= OptionMenu(root,prpp,*dice)
dd3= OptionMenu(root,prpp,*dice)
dd4= OptionMenu(root,prpp,*dice)
dd5= OptionMenu(root,prpp,*dice)
dd6= OptionMenu(root,prpp,*dice)
#setting layout
button1.grid(row=0,column=0)
label1.grid(row=1,column=0)
label2.grid(row=2,column=0)
label3.grid(row=3,column=0)
label4.grid(row=4,column=0)
label5.grid(row=5,column=0)
label6.grid(row=6,column=0)
dd1.grid(row=1, column=1)
dd2.grid(row=2,column=1)
dd3.grid(row=3,column=1)
dd4.grid(row=4,column=1)
dd5.grid(row=5,column=1)
dd6.grid(row=6,column=1)
root.mainloop()
So i'm acctually lost for ideas on what to do since i am fairly new to python. Only thing i could think of is creating the dropdown menus within the "diceroll" button definition but that is not reall what would want to do. Thanks in advance.
edit: fixed spelling.
Is this is what you want? I moved optionmenu
into roll()
function
from random import randint
from tkinter import *
root = Tk()
root.title('Hobbyprojekt')
count = -1
#global dice
dice = [0]
prpp= IntVar()
diceshow=Label()
#defining funtions for buttons
def roll():
global count
global diceshow
global dice
count +=1
print(count)
if count >= 1:
dice=[]
for x in range (0,7) :
dice.append(randint(1,10))
#calculating the viable dice options
for x in range (0,2) :
dice.remove(min(dice))
if count >= 1:
diceshow.destroy()
print("destroyed")
diceshow=Label(root, text=dice)
diceshow.grid(row=0,column=1)
print(dice)
print(dice[1])
print(dice[2])
print(dice[3])
dd1= OptionMenu(root,prpp,dice[0])
dd2= OptionMenu(root,prpp,dice[1])
dd3= OptionMenu(root,prpp,dice[2])
dd4= OptionMenu(root,prpp,dice[3])
dd5= OptionMenu(root,prpp,dice[4])
dd6= OptionMenu(root,prpp,dice[5])
dd1.grid(row=1, column=1)
dd2.grid(row=2,column=1)
dd3.grid(row=3,column=1)
dd4.grid(row=4,column=1)
dd5.grid(row=5,column=1)
dd6.grid(row=6,column=1)
#setting up the test gui
button1 = Button(root, text='Roll the dice', command=roll)
label1= Label(text='choice1')
label2= Label(text='choice2')
label3= Label(text='choice3')
label4= Label(text='choice4')
label5= Label(text='choice5')
label6= Label(text='choice6')
#setting layout
button1.grid(row=0,column=0)
label1.grid(row=1,column=0)
label2.grid(row=2,column=0)
label3.grid(row=3,column=0)
label4.grid(row=4,column=0)
label5.grid(row=5,column=0)
label6.grid(row=6,column=0)
root.mainloop()
Result before:
Result after: