Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Grid_Destory() Not Working
#7
(Mar-04-2021, 03:21 PM)deanhystad Wrote: I tried "quit" and it complained that "Quitter" object had no attribute '__name__'. You program wasn't exiting after a few seconds, it was crashing. I did find that 'root.destroy' worked fine.

It is easy to specify arguments in a callback function. This example uses lambda expressions. There is also the "partial" function in the functools library. Both are very useful and you should know how to use them if you want to write GUI applications.

This is your code without duplicate code. Only one "click" function required to handle all 9 buttons. Your buttons are nearly identical too, so I make them in a loop.
import sys
import pathlib
import random
import tkinter as tk

IMAGE_DIR = pathlib.Path(sys.path[0])
font = ('Helvetica', 32, 'bold')

def click(player_choice):
    if player_choice == random_num:
        message['text'] = "Correct Guess - Well Done"
        root.after(5000, root.destroy)
    else:
        hearts.pop(-1).grid_forget()
        
        lives = len(hearts)
        if lives > 0:
            message['text'] = f"You have {lives} lives left"
        else:
            message['text'] = "GAME OVER! - Thanks for playing"
            root.after(5000, root.destroy)

root = tk.Tk()
root.title("Game")
random_num = random.randint(1, 9)

message = tk.Label(text=f"Try to guess the number {random_num}")
message.grid(row=0, column=0, columnspan=4, padx=10, pady=10)

for i in range(9):
    button = tk.Button(root, text=str(i+1), font=font, command=lambda number=i+1: click(number))
    button.grid(row=i // 3 + 1, column=i % 3, padx=10, pady=10)
 
heart_images = [
    tk.PhotoImage(file=IMAGE_DIR/'ttt_x.png'),
    tk.PhotoImage(file=IMAGE_DIR/'ttt_o.png'),
    tk.PhotoImage(file=IMAGE_DIR/'ttt_x.png')]

hearts = []
for i, image in enumerate(heart_images):
    heart = tk.Label(root, image=image)
    heart.grid(row=i+1, column=4)
    hearts.append(heart)
 
root.mainloop()
I used some image files I had laying around. When referencing files you should treat the filename and the file path independently. Otherwise you have no hope of ever running your programs on another computer or just surviving a minor directory reorganization. In this example I'm using a trick to set IMAGE_DIR to the folder that contains the current python module (program we are running).


thanks!
Reply


Messages In This Thread
Grid_Destory() Not Working - by finndude - Mar-03-2021, 06:53 PM
RE: Grid_Destory() Not Working - by bowlofred - Mar-03-2021, 09:18 PM
RE: Grid_Destory() Not Working - by finndude - Mar-04-2021, 09:43 AM
RE: Grid_Destory() Not Working - by deanhystad - Mar-04-2021, 02:05 PM
RE: Grid_Destory() Not Working - by finndude - Mar-04-2021, 02:18 PM
RE: Grid_Destory() Not Working - by deanhystad - Mar-04-2021, 03:21 PM
RE: Grid_Destory() Not Working - by finndude - Mar-04-2021, 05:11 PM

Forum Jump:

User Panel Messages

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