I want to build a simple dice rolling app (personal project to get better with python) in which the user can select whether to roll a 6 or 20 sided dice using tkinter.
Basically, there should be 4 widgets: a label (to print the result) a button (to run the dice rolling function) and 2 checkboxes (to select which dice to roll)
so the code I have tried looks like this.
from tkinter import *
import tkinter as tk
import random
window = tk.Tk()
def roll():
if var1 == 1:
print("your result is " + str(random.randint(1,6)))
elif var2 == 1:
print("your result is " + str(random.randint(1,20)))
label = tk.Label(canvas, text = roll(), width = 20, font = 40,
height = 1)
label.place(relx=.5, rely = .2, anchor='n')
var1 = IntVar()
var2 = IntVar()
canvas = tk.Canvas(window, height = 600, width = 300, bg = 'blue').pack()
C1 = Checkbutton(canvas, text = "6", variable = var1.get(),
onvalue = 1, offvalue = 0, height=1,
width = 10)
C1.place(relx = .01, rely = .7)
C2 = Checkbutton(canvas, text = "20", variable = var2.get(),
onvalue = 1, offvalue = 0, height=1,
width = 10)
C2.place(relx = .5, rely = .7)
btn = tk.Button(canvas, text = 'roll dem bones!', command = lambda:
roll())
btn.place(relx = .5, rely = .9)
window.mainloop()
I have two problems so far.
1: when I click either CheckButtons a tick appears in both, meaning both are getting activated (this is probably something I can fix just by getting better with tkinter, but if you spot the problem I'd appreciate the advice.
2: when I click the button I get the error "RecursionError: maximum recursion depth exceeded in comparison".
I think the issue is that I just can't get my head around calling functions in this way with tkinter. I'm actually using this as an exercise to build a more complex RPG app to use with some friends where you would choose the attribute bonus to add to a D20 roll, but I wanted to try something that was more simple without the rest of the app in the code.
The python community so far has been the most helpful and least dismissive of all the coding groups I've sought help from so thanks in advance for the help.
Kev.