Brandon
Brandon

Reputation: 3

Why can't I open Tk in tkinter?

I coded this game myself. It is a guessing game regarding 2 players.

from random import randint
from tkinter import *

p1 = 0 
p2 = 0
while p1 < 3 and p2 < 3:
 answer = randint(0,16)
 wrong_answer = randint(0,16)
 wrong_answer1 = randint(0,16)


 s_list =    ["apple","book","phone","sheep","ruler","pen","eraser","knife","cement","Google","file","stapler","thermometer","box","glue","yes","no"]
 hint_list = ["Fruit","Read","Technology","Animal","Measure Length","Writing","Stationary","Cut  food", "Building Material","Search Engine","Paper organizer","Binding papers together","Temperature","Storage","Attach things together","Approval","Disapproval"]
 secret_word = s_list[answer]
 hint = f'Hint:{hint_list[answer]}'
 incorrect = s_list[answer]
 incorrect1 = s_list[answer]


 master = Tk()
 master.title("2 Players Guessing Game!")
 master.geometry('700x900+90+90')

 def random1():
   global p1
   p1 += 1
   label4["text"] = "Correct! Score:",p1 ,p2

 def random2():
   global p2
   p2 += 1
   label4["text"] = "Incorrect! Score:",p1 ,p2

 def random3():
   global p2
   p2 += 1
   label4["text"] = "Correct! Score:",p1 ,p2

 def random4():
   global p1
   p1 += 1
   label4["text"] = "Incorrect! Score:",p1 ,p2

 label = Label(master, text="2 Player Guessing Game!", font  = "Arial 14")
 label2 = Label(master, text="<- P1", font = "Arial 14")
 label3 = Label(master, text="P2 ->", font = "Arial 14")
 button = Button(master, text=secret_word, font = "Arial 14", command=random1)
 button2 = Button(master, text=incorrect, font = "Arial 14", command=random2)
 button3 = Button(master, text=incorrect1, font = "Arial 14", command=random2)
 button4 = Button(master, text=secret_word, font = "Arial 14", command=random3)
 button5 = Button(master, text=incorrect, font = "Arial 14", command=random4)
 button6 = Button(master, text=incorrect1, font = "Arial 14", command=random4)
 label4 = Label(master, text=hint, font = "Arial 14")


 label.pack()
 label2.pack()
 label3.pack()
 label4.pack()
 button.pack(side=LEFT)
 button2.pack(side=LEFT)
 button3.pack(side=LEFT)
 button4.pack(side=RIGHT)
 button5.pack(side=RIGHT)
 button6.pack(side=RIGHT)



 if p1 == 3:
   label["text"] = "Player 1 won!"
   exit()
 elif p2 == 3:
   label["text"] = "Player 2 won!"
   exit()
 else:
   continue

Before I added this "continue" , I could print Tk properly but could not loop. After adding "continue", my Tk can't even load now. Why? Please help. Thanks.

mainloop()

The reason why I did not use PyGame is because I wanted my game to be a "Answer first, get point" type of game.

Upvotes: 0

Views: 138

Answers (4)

acw1668
acw1668

Reputation: 47219

You should note that tkinter is event-driven, and you should not apply procedural/sequential programming on it.

For your case, you don't need the while loop and you should put the checking winner logic in a function and call it inside those randomX() functions:

from random import randint
from tkinter import *

p1 = 0 
p2 = 0

answer = randint(0,16)
wrong_answer = randint(0,16)
wrong_answer1 = randint(0,16)

s_list =    ["apple","book","phone","sheep","ruler","pen","eraser","knife","cement","Google","file","stapler","thermometer","box","glue","yes","no"]
hint_list = ["Fruit","Read","Technology","Animal","Measure Length","Writing","Stationary","Cut  food", "Building Material","Search Engine","Paper organizer","Binding papers together","Temperature","Storage","Attach things together","Approval","Disapproval"]
secret_word = s_list[answer]
hint = f'Hint:{hint_list[answer]}'
incorrect = s_list[answer]
incorrect1 = s_list[answer]

master = Tk()
master.title("2 Players Guessing Game!")
master.geometry('700x900+90+90')

def check_winner():
    winner = 1 if p1 == 3 else 2 if p2 == 3 else None
    if winner:
        print(f'Player {winner} won!')
        master.destroy() # end the program

def random1():
   global p1
   p1 += 1
   label4["text"] = "Correct! Score:",p1 ,p2
   check_winner()

def random2():
   global p2
   p2 += 1
   label4["text"] = "Incorrect! Score:",p1 ,p2
   check_winner()

def random3():
   global p2
   p2 += 1
   label4["text"] = "Correct! Score:",p1 ,p2
   check_winner()

def random4():
   global p1
   p1 += 1
   label4["text"] = "Incorrect! Score:",p1 ,p2
   check_winner()

label = Label(master, text="2 Player Guessing Game!", font  = "Arial 14")
label2 = Label(master, text="<- P1", font = "Arial 14")
label3 = Label(master, text="P2 ->", font = "Arial 14")
button = Button(master, text=secret_word, font = "Arial 14", command=random1)
button2 = Button(master, text=incorrect, font = "Arial 14", command=random2)
button3 = Button(master, text=incorrect1, font = "Arial 14", command=random2)
button4 = Button(master, text=secret_word, font = "Arial 14", command=random3)
button5 = Button(master, text=incorrect, font = "Arial 14", command=random4)
button6 = Button(master, text=incorrect1, font = "Arial 14", command=random4)
label4 = Label(master, text=hint, font = "Arial 14")

label.pack()
label2.pack()
label3.pack()
label4.pack()
button.pack(side=LEFT)
button2.pack(side=LEFT)
button3.pack(side=LEFT)
button4.pack(side=RIGHT)
button5.pack(side=RIGHT)
button6.pack(side=RIGHT)

master.mainloop()

Upvotes: 0

andypaling1
andypaling1

Reputation: 142

You need to call mainloop() at some point to get the window to show, your cord should look something like this:


from random import randint
from tkinter import *

p1 = 0 
p2 = 0
while p1 < 3 and p2 < 3:
 answer = randint(0,16)
 wrong_answer = randint(0,16)
 wrong_answer1 = randint(0,16)


 s_list =    ["apple","book","phone","sheep","ruler","pen","eraser","knife","cement","Google","file","stapler","thermometer","box","glue","yes","no"]
 hint_list = ["Fruit","Read","Technology","Animal","Measure Length","Writing","Stationary","Cut  food", "Building Material","Search Engine","Paper organizer","Binding papers together","Temperature","Storage","Attach things together","Approval","Disapproval"]
 secret_word = s_list[answer]
 hint = f'Hint:{hint_list[answer]}'
 incorrect = s_list[answer]
 incorrect1 = s_list[answer]


 master = Tk()
 master.title("2 Players Guessing Game!")
 master.geometry('700x900+90+90')

 def random1():
   global p1
   p1 += 1
   label4["text"] = "Correct! Score:",p1 ,p2

 def random2():
   global p2
   p2 += 1
   label4["text"] = "Incorrect! Score:",p1 ,p2

 def random3():
   global p2
   p2 += 1
   label4["text"] = "Correct! Score:",p1 ,p2

 def random4():
   global p1
   p1 += 1
   label4["text"] = "Incorrect! Score:",p1 ,p2

 label = Label(master, text="2 Player Guessing Game!", font  = "Arial 14")
 label2 = Label(master, text="<- P1", font = "Arial 14")
 label3 = Label(master, text="P2 ->", font = "Arial 14")
 button = Button(master, text=secret_word, font = "Arial 14", command=random1)
 button2 = Button(master, text=incorrect, font = "Arial 14", command=random2)
 button3 = Button(master, text=incorrect1, font = "Arial 14", command=random2)
 button4 = Button(master, text=secret_word, font = "Arial 14", command=random3)
 button5 = Button(master, text=incorrect, font = "Arial 14", command=random4)
 button6 = Button(master, text=incorrect1, font = "Arial 14", command=random4)
 label4 = Label(master, text=hint, font = "Arial 14")


 label.pack()
 label2.pack()
 label3.pack()
 label4.pack()
 button.pack(side=LEFT)
 button2.pack(side=LEFT)
 button3.pack(side=LEFT)
 button4.pack(side=RIGHT)
 button5.pack(side=RIGHT)
 button6.pack(side=RIGHT)

 master.mainloop()

Tkinter understanding mainloop I hope this helps

Upvotes: 1

Roland Smith
Roland Smith

Reputation: 43573

GUI toolkits like tkinter are event-driven. To work properly, the mainloop must continuously be able to process keyboard and mouse events.

So they work quite differently from normal Python scripts that just run from top to bottom.

A tkinter program runs within the mainloop. So there are only three things you do before starting the mainloop.

  • Create a window with some widgets on it.
  • Initialize global program state.
  • Define functions that can be run from the mainloop as callbacks or idle tasks.

A callback is called in response to activating a control (like clicking on a button).

An idle task is started by the system after a specified number of milliseconds when the system is not busy processing events. You can schedule idle tasks with the Tk.after() method.

Basically, the callbacks and idle tasks are your program.

Upvotes: 0

Zack Turner
Zack Turner

Reputation: 226

Have you tried adding master.mainloop() at the end and getting rid of continue

from random import randint
from tkinter import *

p1 = 0
p2 = 0
while p1 < 3 and p2 < 3:
 answer = randint(0,16)
 wrong_answer = randint(0,16)
 wrong_answer1 = randint(0,16)


 s_list =    ["apple","book","phone","sheep","ruler","pen","eraser","knife","cement","Google","file","stapler","thermometer","box","glue","yes","no"]
 hint_list = ["Fruit","Read","Technology","Animal","Measure Length","Writing","Stationary","Cut  food", "Building Material","Search Engine","Paper organizer","Binding papers together","Temperature","Storage","Attach things together","Approval","Disapproval"]
 secret_word = s_list[answer]
 hint = f'Hint:{hint_list[answer]}'
 incorrect = s_list[answer]
 incorrect1 = s_list[answer]


 master = Tk()
 master.title("2 Players Guessing Game!")
 master.geometry('700x900+90+90')

 def random1():
   global p1
   p1 += 1
   label4["text"] = "Correct! Score:",p1 ,p2

 def random2():
   global p2
   p2 += 1
   label4["text"] = "Incorrect! Score:",p1 ,p2

 def random3():
   global p2
   p2 += 1
   label4["text"] = "Correct! Score:",p1 ,p2

 def random4():
   global p1
   p1 += 1
   label4["text"] = "Incorrect! Score:",p1 ,p2

 label = Label(master, text="2 Player Guessing Game!", font  = "Arial 14")
 label2 = Label(master, text="<- P1", font = "Arial 14")
 label3 = Label(master, text="P2 ->", font = "Arial 14")
 button = Button(master, text=secret_word, font = "Arial 14", command=random1)
 button2 = Button(master, text=incorrect, font = "Arial 14", command=random2)
 button3 = Button(master, text=incorrect1, font = "Arial 14", command=random2)
 button4 = Button(master, text=secret_word, font = "Arial 14", command=random3)
 button5 = Button(master, text=incorrect, font = "Arial 14", command=random4)
 button6 = Button(master, text=incorrect1, font = "Arial 14", command=random4)
 label4 = Label(master, text=hint, font = "Arial 14")


 label.pack()
 label2.pack()
 label3.pack()
 label4.pack()
 button.pack(side=LEFT)
 button2.pack(side=LEFT)
 button3.pack(side=LEFT)
 button4.pack(side=RIGHT)
 button5.pack(side=RIGHT)
 button6.pack(side=RIGHT)

 if p1 == 3:
   label["text"] = "Player 1 won!"
   exit()
 elif p2 == 3:
   label["text"] = "Player 2 won!"
   exit()

 master.mainloop()

This works for me.

Upvotes: 1

Related Questions