Python Forum

Full Version: help please, checking the user input and outputting the result
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi, im trying to learn tkinter and i want to create a number guessing game where you can choose difficulty but i have problems with checking the user input and printing if user has to guess higher, lower or if he is correct, cand you help me please?

here is the code:

from tkinter import *
from tkinter import ttk
import random

root=Tk()
root.geometry("500x175")
root.title("Úroveň")
#rooot2=Tk()



################################################################################

################################################################################

def dva():
    global ur_2
    ur_2 = random.randint(1,100)
   
 #DONT CARE ABOUT THIS ONE
def tri():
    global ur_3
    ur_3 = random.randint(1,500)

#DONT CARE ABOUT THIS ONE
def koniec():
    #root2.destroy()
    Label(root2,text="Gratulujem, vyhral si!").pack(fill=BOTH)
    Button(root2,text="Znova?", command=root.iconify()).pack(fill=BOTH)
    Button(root2,text="Koniec", fg="red",command=root2.destroy()).pack(fill=BOTH)

def start1():
    global t2
    root.destroy()
    root1=Tk()
    root1.geometry("250x175")
    root1.title("Hra")
    
    global ur_1
    ur_1 = random.randint(1,10)
    
    l=Label(root1, text="Tipni si číslo", font=("Helvetica", 25)).pack()                                                                  
    e1=Entry(root1, width=15).pack()            
    b=Button(root1,text="OK", width=5, command=check1).pack()
    l1=Label(root1).pack(fill=BOTH)

#DONT CARE ABOUT THIS ONE
def start2():
    global t2
    root.destroy()
    root1=Tk()
    root1.geometry("250x175")
    root1.title("Hra")
    
    l=Label(root1, text="Tipni si číslo", font=("Helvetica", 25)).pack()                                                                  
    e1=Entry(root1, width=15).pack()
    b=Button(root1,text="OK", width=5, command=check2).pack()
    l1=Label(root1, text="NIŽŠIE/VYŠŠIE").pack(fill=BOTH)

#DONT CARE ABOUT THIS ONE
def start3():
    global t2
    root.destroy()
    root1=Tk()
    root1.geometry("250x175")
    root1.title("Hra")
    
    l=Label(root1, text="Tipni si číslo", font=("Helvetica", 25)).pack()                                                                  
    e1=Entry(root1, width=15).pack()
    b=Button(root1,text="OK", width=5, command=check3).pack()
    l1=Label(root1, text="NIŽŠIE/VYŠŠIE").pack(fill=BOTH)   
    
Label(root,text="Vyber si obtiažnosť", font=("Helvetica", 30)).pack(fill=BOTH)
Button(root,text="Úroveň 1 (1-10)", fg="green", width=25, command=start1).pack(fill=BOTH)
Button(root,text="Úroveň 2 (1-100)", fg="orange",width=25, command=start2).pack(fill=BOTH)
Button(root,text="Úroveň 3 (1-500)", fg="red", width=25, command=start3).pack(fill=BOTH)
    
root.mainloop()
root1.mainloop()
#root2.mainloop()
You don't have any of the command functions, see here http://effbot.org/tkinterbook/button.htm Also, you can only declare Tk() once without possible problems. If you want more than one window use a Toplevel, look on effbot.org Finally, pack() returns None, so b, e, l, etc is None on lines like this
b=Button(root1,text="OK", width=5, command=check1).pack()
so see https://books.google.com/books?id=q8W3WQ...ne&f=false and the pack() geometry manager on effbot as well. https://wiki.python.org/moin/TkInter
FYI: Although dated, this reference is still totally valid as tkinter hasn't changed much if any, and in
my opinion, it's one if not the best: http://infohost.nmt.edu/tcc/help/pubs/tk...index.html
pdf here: http://www.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf
I quickly reviewed your code and have a couple observations:
Make a class and eliminate your globals
initiate Tk once not 3 times:
you have multiple ways to create other free standing widgets:
create a separate frames
use simpledialogs-
example:
from tkinter.simpledialog import *
from tkinter import *
if __name__ == '__main__':

    def test():
        root = Tk()
        def doit(root=root):
            d = SimpleDialog(root,
                         text="This is a test dialog.  "
                              "Would this have been an actual dialog, "
                              "the buttons below would have been glowing "
                              "in soft pink light.\n"
                              "Do you believe this?",
                         buttons=["Yes", "No", "Cancel"],
                         default=0,
                         cancel=2,
                         title="Test Dialog")
            print(d.go())
            print(askinteger("Spam", "Egg count", initialvalue=12*12))
            print(askfloat("Spam", "Egg weight\n(in tons)", minvalue=1,
                           maxvalue=100))
            print(askstring("Spam", "Egg label"))
        t = Button(root, text='Test', command=doit)
        t.pack()
        q = Button(root, text='Quit', command=t.quit)
        q.pack()
        t.mainloop()

    test()
this example is from the simpledialog module. you could-
guess= askinteger("your guess", "Number 1-10", initialvalue=1)

then compare guess to your random number...