Python Forum
[Tkinter] help please, checking the user input and outputting the result
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] help please, checking the user input and outputting the result
#1
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()
Reply
#2
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
Reply
#3
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
Reply
#4
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...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGUI] [Solved]Help storing in user input from line edit Extra 2 1,680 May-12-2022, 07:46 PM
Last Post: Extra
  Convert combobox user input in to date with tkinter Ame 8 6,659 Jul-01-2020, 09:40 PM
Last Post: Yoriz
  Create an identification code from user input PeroPuri 1 1,870 Apr-11-2020, 11:56 AM
Last Post: Larz60+
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 13,666 Mar-20-2020, 07:21 AM
Last Post: deanhystad
  PyQt5: How do you set the user input of a line edit to a specific variable? YoshikageKira 17 11,471 Dec-26-2019, 03:18 PM
Last Post: Denni
  [Tkinter] Is there a way to sleep without stopping user input? GalaxyCoyote 2 2,098 Oct-23-2019, 06:23 PM
Last Post: Denni
  [PyGUI] Hi All, how to hide/mask user input in PySimpleGUI nmrt 1 14,838 Sep-21-2018, 09:59 AM
Last Post: nmrt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020