0

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.

1
  • I also promise that I have tried multiple tkinter tutorials and reading the docs but I just can't get my head around this. Commented Jul 5, 2019 at 19:14

1 Answer 1

1

There are many things wrong with your code, but let's just focus on the questions you asked. The first problem comes from your mis-use of IntVar: you created two IntVar, but you set the variable of your Checkbuttons to var1.get(). get is only a method to return the value set to your IntVar. To correctly associate the variables:

C1 = Checkbutton(canvas, text = "6", variable = var1,
                 onvalue = 1, offvalue = 0, height=1,
                 width = 10)

Same for the other Checkbutton.

Next, the error comes from your function roll. You created a label with text=roll(), which executes the roll function again, and then also executes itself again... which results in your maximum recursion error.

To properly fix only the roll part, you can define another function which returns the result within roll:

def roll():
    def get_result():
        if var1.get() == 1:
            return "your result is " + str(random.randint(1,6))
        elif var2.get() == 1:
            return "your result is " +  str(random.randint(1,20))
    label = tk.Label(canvas, text = get_result(), width = 20, font = 40,
                 height = 1)
    label.place(relx=.5, rely = .2,  anchor='n')

Notice that I have changed if var1 == 1: to if var1.get() ==1: as explained earlier.

You still have other problems like stacking new labels upon each click, redundant use of lambda function, the variable canvas being set to None... but I'll leave those for you to fix by yourself.

Sign up to request clarification or add additional context in comments.

2 Comments

So I just wanna say thanks a million for taking the time to write this answer. I know my code is shoddy as I'm coming to all this with no coding experience and just trying to work my way through it, learning by making mistakes and then asking for help if you get me. Thanks a million Henry.
No problem. If it helped, consider accepting the answer so the question can be closed. Also a small piece of advice: if you go to the "frequent" tab under questions, you will see some questions that new learners are bound to encounter (e,g. images not showing, command not executing...). Reading them as you learn will be a big advantage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.