Jun-08-2020, 10:00 PM
Is there anyway to get a frame name/properties? I've tried the root.children.values and get .!frame. But just was wondering how to get what the frame has been named. I hope what I'm asking makes sense.
#! /usr/bin/env python3 '''tkinter tic tac toe''' import tkinter as tk import random as rnd from time import sleep class Game: def __init__(self, parent): # Set the parent self.parent = parent # Storage for entries self.entries = [] # Grid cordinates self.grid_cords = [ (0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2) ] for i in range(3): for j in range(3): self.frame = tk.Frame(self.parent, borderwidth=1, relief='solid', \ cursor='hand2', width=100, height=100) self.frame.grid(column=i, row=j) self.frame.bind('<Button-1>', self.get_cords) def do_entries(self, text,col,row, fg='red'): label = tk.Label(self.parent, fg=fg, text=text, font=('serif', 60)) label.grid(column=col, row=row) self.entries.append(label) def get_cords(self, event): x_cord = event.x_root - self.parent.winfo_rootx() y_cord = event.y_root - self.parent.winfo_rooty() loc = self.parent.grid_location(x_cord, y_cord) self.do_entries('X',loc[0],loc[1]) self.grid_cords.remove(loc) self.computer() def computer(self): pick = rnd.choice(self.grid_cords) self.grid_cords.remove(pick) self.do_entries('O',pick[0], pick[1],fg='blue') root = tk.Tk() Game(root) root.mainloop()
import tkinter as tk from functools import partial class Game: def __init__(self, parent): self.parent = parent self.entries = [] for x in range(3): for y in range(3): self.frame = tk.Frame(self.parent, borderwidth=1, relief='solid', \ cursor='hand2', width=100, height=100) self.frame.grid(column=x, row=y) self.letter = 'X' self.frame.bind('<Button-1>', lambda e, a=x, b=y: self.set_square(a, b)) def set_square(self, x, y): if len(self.entries) % 2 == 0: label = tk.Label(self.parent, fg='red', text='X', font=('serif', 60)) else: label = tk.Label(self.parent, fg='blue', text='O', font=('serif', 60)) label.grid(column=x, row=y) self.entries.append(label) root = tk.Tk() Game(root) root.mainloop()