Posts: 1,145
Threads: 114
Joined: Sep 2019
Jun-08-2020, 10:00 PM
(This post was last modified: Jun-08-2020, 10:00 PM by menator01.)
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.
Posts: 12,030
Threads: 485
Joined: Sep 2016
Posts: 1,145
Threads: 114
Joined: Sep 2019
Jun-09-2020, 02:48 AM
(This post was last modified: Jun-09-2020, 04:04 AM by menator01.)
Yes. I decided to take a different approach which works.
To show what I am working on I'll post the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import tkinter as tk
import random as rnd
from time import sleep
class Game:
def __init__( self , parent):
self .parent = parent
self .entries = []
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()
|
Posts: 6,788
Threads: 20
Joined: Feb 2020
Or use a lambda
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
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()
|
|