Tkinter How to make listbox appear over a frame - python-3.x

making an F1 application where users can type in a driver from the current grid [supported via autofill/suggestions], and then API fetches relevant stats
The issue I'm having is that I want the listbox to appear over another frame (called left_frame). However, what this does is that the listbox goes under left_frame , and not over it. Any thoughts of how to make it appear over the frame?
some approaches that I tried:
used .lift(), but .lift() only seems to work when it's relative to objects in the same frame
since I currently use .grid and placed the listbox in the root frame, I initially tried to put it in top_frame, but it then expands the top_frame. Since I have positioned all frames using .place(), I don't think I can use propagate(False) (I tried it , didn't work)
Here's a picture of the issues
Here's my code so far:
#import necessary modules
from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image
from tkinter import messagebox
import requests
import json
#set up main window components
root = Tk()
root.title("F1 Desktop Application")
root.geometry("500x600")
root.configure(bg="white")
#since same directory, can just use filename
root.iconbitmap("formula1_logo.ico")
#images
#creating canvas for image to go into
#canvas_f1_logo = Canvas(top_frame, width = 5, height = 5)
#canvas.grid(row = 0, column = 1)
#acquire image
#formula1_logo = ImageTk.PhotoImage(Image.open("formula1_logo.png"))
#resize image
#resized_formula1_logo = formula1_logo.resize((3,3), Image.ANTIALIAS)
#new_formula1_logo = ImageTK.PhotoImage(resized_formula1_logo)
#canvas.create_image(5,5, anchor = NW, image = new_formula1_logo)
#functions
#generate 2022 drivers-list [can scale to drivers-list by changing the]
drivers_list_request = requests.get("http://ergast.com/api/f1/2022/drivers.json")
#initialize empty-list
drivers_list = []
drivers_list_object = json.loads(drivers_list_request.content)
for elements in drivers_list_object["MRData"]["DriverTable"]["Drivers"]:
drivers_list.append(elements["givenName"] + " " + elements["familyName"])
#set up frames [main frames, can put frames within frames if needed]
#frame for search bar + magnifying glass
top_frame = LabelFrame(root, padx = 80, pady = 15)
top_frame.place(relx = 0.5, rely = 0.2, anchor = CENTER)
header_label = Label(top_frame, text = "F1 2022 Drivers App", pady = 20, font = ("Arial bold",14))
header_label.grid(row = 0, column = 0, pady = 2 )
search_button = Button(top_frame, text = "search", padx = 2, pady = 2)
search_button.grid(row = 1, column = 1)
# Update the Entry widget with the selected item in list
def check(e):
v= entry.get()
if v=='':
hide_button(menu)
else:
data=[]
for item in drivers_list:
if v.lower() in item.lower():
data.append(item)
update(data)
show_button(menu)
def update(data):
# Clear the Combobox
menu.delete(0, END)
# Add values to the combobox
for value in data:
menu.insert(END,value)
def fillout(event):
try:
entry.delete(0,END)
entry.insert(0,menu.get(menu.curselection()))
#handle a complete deletion of entry-box via cursor double tap
except:
pass
def hide_button(widget):
widget.grid_remove()
def show_button(widget):
widget.grid()
# Create an Entry widget
entry= Entry(top_frame)
entry.grid(row = 1, column = 0)
entry.bind('<KeyRelease>',check)
# Create a Listbox widget to display the list of items
menu= Listbox(root)
menu.grid(row = 2, column = 0, padx = 165, pady = 165)
menu.bind("<<ListboxSelect>>",fillout)
menu.lift()
# Add values to our combobox
hide_button(menu)
left_frame = LabelFrame(root, padx = 30, pady = 30)
left_frame.place(relx = 0.24, rely = 0.5, anchor = CENTER)
bottom_left_frame = LabelFrame(root, padx = 30, pady = 30)
bottom_left_frame.place(relx = 0.24, rely = 0.82, anchor = CENTER)
bottom_right_frame = LabelFrame(root, padx = 30, pady = 30)
bottom_right_frame.place(relx = 0.6, rely = 0.82)
basic_info = Label(left_frame, text = "Basic Info ", font = ("Arial bold",14))
basic_info.grid(row = 0, column = 0, pady = 3)
full_name = Label(left_frame, text = "Full Name : ")
full_name.grid(row = 1, column = 0, pady = 2)
driver_code = Label(left_frame, text = "Driver Code : ")
driver_code.grid(row = 2, column = 0, pady = 2)
nationality = Label(left_frame, text = "Nationality : ")
nationality.grid(row = 3, column = 0, pady = 2)
F1_career = Label(bottom_left_frame, text = "F1 Career ", font = ("Arial bold",14))
F1_career.grid(row = 0, column = 0, pady = 3)
wins = Label(bottom_left_frame, text = "Wins :")
wins.grid(row = 1, column = 0, pady = 2)
poles = Label(bottom_left_frame, text = "Poles :")
poles.grid(row = 2, column = 0, pady = 2)
drivers_championships = Label(bottom_left_frame, text = "Championships :")
drivers_championships.grid(row = 3, column = 0 , pady = 2)
F1_22_stats = Label(bottom_right_frame, text = "F1 22 Stats", font = ("Arial bold",14))
F1_22_stats.grid(row = 0, column = 0, pady = 3)
root.mainloop()
would appreciate the help!

Related

Tkinter button displays center under a ttk.text

I have a small notepad that I'm creating for myself.
However, in the section of "Troubleshooting Steps" I have a TTK.text that is takint a wide column. Now I need to add two buttons underneath. "Save" and "Clear"
The "save" button shows center under the TEXT box. (Expected) but when I try to add "Clear" next to "Save" it is displayed in a different column way to the rigth.
Here is the code below.
from tkinter import *
from tkinter import ttk
from tkinter import Text
class Example(Frame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("Inbound Mail")
self.pack(fill=BOTH, expand=True)
frame = LabelFrame(self, text = "CUSTOMER INFORMTION ")
frame.grid(row =0, column = 0, pady = 20, padx = 10)
case_number = Label(frame, text = "Case Number ")
case_number.grid(row = 0, column = 0, pady = 3, padx = 3)
self.case_entry = Entry(frame, width = 30)
self.case_entry.grid(row = 0, column = 1, pady = 5)
customer_name = Label(frame, text = "Contact Name ")
customer_name.grid(row =1, column = 0, pady = 3, padx = 3)
self.name_entry = Entry(frame, width = 30)
self.name_entry.grid(row = 1, column = 1)
customer_phone = Label(frame, text = "Phone Number ")
customer_phone.grid(row =2, column = 0, pady = 3, padx = 3)
self.phone_entry = Entry(frame, width = 30)
self.phone_entry.grid(row = 2, column = 1)
customer_email = Label(frame, text = "Email Address ")
customer_email.grid(row =3, column = 0, pady = 3, padx = 3)
self.email_entry = Entry(frame, width = 30)
self.email_entry.grid(row = 3, column = 1)
Label(frame, text = "Product").grid(row = 4, column = 0, pady = 3, padx = 3)
self.product = ttk.Combobox(frame,
values = [
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8"], width = 27)
self.product.grid(row = 4, column = 1, pady = 3, padx = 3)
self.product.current(0)
#ISSUE DESCRIPTION
frame2 = LabelFrame(self, text = "ISSUE")
frame2.grid(row = 5 , column = 0, pady = 20, padx = 10)
Label(frame2, text = "ISSUE DESCRIPTION").grid(row = 6, column = 0, pady = 3, padx = 3)
self.issue = Entry(frame2, width = 55)
self.issue.grid(row = 7, column = 0, pady = 3, padx = 3)
# TROUBLESHOOTING STEPS
frame3 = LabelFrame(self, text = "TROUBLESHOOTING STEPS")
frame3.grid(row = 8, column = 0)
self.troubles = Text(frame3, height = 15, width = 40, wrap = WORD)
self.troubles.grid(row = 9, column = 0,padx = 10, pady = 10)
#SCROLLBAR FOR TEXT
scroll = Scrollbar(self, command = self.troubles.yview)
scroll.grid(row = 8, column = 1, sticky = "nsew")
self.troubles.config(yscrollcommand = scroll.set)
#SAVE BUTTON AND CLEAR BUTTON.
self.save = Button(frame3, text = "Save")
self.save.grid(row = 10, column = 0)
def main():
root = Tk()
app = Example()
root.geometry("380x650")
# root.resizable(False, False)
root.mainloop()
if __name__ == '__main__':
main()
Make another small Frame to act as a container for the Buttons. You may also want to know that a scrolled Text widget is part of standard tkinter, you don't have to reinvent that. Just add this import:
from tkinter.scrolledtext import ScrolledText
And then:
# TROUBLESHOOTING STEPS
frame3 = LabelFrame(self, text = "TROUBLESHOOTING STEPS")
frame3.grid(row = 8, column = 0)
self.troubles = ScrolledText(frame3, height = 15, width = 40, wrap = WORD)
self.troubles.grid(row = 9, column = 0,padx = 10, pady = 10)
#SAVE BUTTON AND CLEAR BUTTON.
buttonframe = Frame(frame3)
buttonframe.grid(row = 10, column = 0)
self.save = Button(buttonframe, text = "Save")
self.save.pack(side=LEFT)
self.clear = Button(buttonframe, text = "Clear")
self.clear.pack(side=LEFT)

TKinter saving user and password in a string file

I am trying to design an interface that allow users to download data with their username and password credentials using a csv file extracted from a database.
The goal is to "kind of" merging connection details with some details from this file to generate CURL sting lines as text file
Here is my csv file example:
Fichier,No. de document,DocumentId,Filename
rar,008_029_ENS_EXE_CCE_PGC_IRE_001,1348828088588073573,NRO-08-029-Saint-Laurent.rar
pdf,08_001_059_DOE_CCR_CNV_SAD_001,1348828088574792249,08_001_059_DOE_CCR_CNV_SAD_001_B3.pdf
pdf,08_001_059_DOE_SRO_LST_LOD_001,1348828088574845667,08_001_059_DOE_SRO_LST_LOD_001_A3.pdf
Here is my "python" script:
import sys
import os
import csv
import subprocess
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
root = Tk()
root.title('Aconex Download Manager')
root.geometry("465x230")
def AddConnection():
if not entry_user.get() or not entry_pass.get():
messagebox.showerror('Invalid', 'Empty username or password')
else:
addconnect = open ("XXXXXX.txt", "w") # don't know what to do in order to keep username and password
addconnect.write(entry_user.get() + ':' + entry_pass.get())
return
def UploadAction():
filename = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("csv files","*.csv"),("all files","*.*")))
def SaveAction(event=None):
directory = filedialog.askdirectory()
def ConvertTool():
data_out = open('download_list.txt', 'w')
with open(filename) as csv_file:
csv_reader = csv.DictReader(csv_file, delimiter=',')
for row in csv_reader:
data_out.write('curl -u '+ (entry_user.get() + ':' + entry_pass.get())' +' -o ' +'"'+ (row['Filename']) +'" '+ 'http://my-website.com/268441485/register/' +""+ (row['DocumentId'])+""+"\n")
data_out.close()
def DownloadTool():
subprocess.call([r'.\bat\download_tool.bat'])
def QuictAction():
root.destroy()
Label(root, text = "Username :").grid(row = 0, padx = 3, pady = 3)
entry_user = Entry(root, width = "30")
entry_user.grid(row = 0, column = 1, padx = 1, pady = 10, ipady = 3)
password = Label(root, text = "Password :").grid(row = 1, padx = 3, pady = 3)
entry_pass = Entry(root,width = "30", show ='*')
entry_pass.grid(row = 1, column = 1, padx = 1, pady = 10, ipady = 3)
button4 = ttk.Button(root, text = ">> Converting Data from CSV", width = 30, command = ConvertTool).grid(row = 3, column = 1, padx = 10, pady = 10, sticky = "nw")
button5 = ttk.Button(root, text = "Download >>", width = 25, command = DownloadTool).grid(row = 3, column = 2, padx = 10, pady = 10, sticky = "ne")
separatorLEFT = ttk.Separator(root, orient = HORIZONTAL).grid(row = 2, column = 1, columnspan = 2, padx = 10, pady = 20, sticky = "ew")
button2 = ttk.Button(root, text = "Select CSV File", width = 25, command = UploadAction).grid(row = 0, column = 2, padx = 10, pady = 10, sticky = "ne")
button3 = ttk.Button(root, text = "Select Download Folder", width = 25, command = SaveAction).grid(row = 1, column = 2, padx = 10, pady = 10, sticky = "ne")
separatorRIGHT = ttk.Separator(root, orient = HORIZONTAL).grid(row = 2, column = 1, columnspan = 2, padx = 10, pady = 20, sticky = "ew")
button6 = ttk.Button(root, text = "Close ", width = 6, command = QuictAction).grid(row = 4, column = 2, padx = 10, pady = 10, sticky = "ne")
root.mainloop()
As you can see, I am not a coder and I am just able to hack a bit codes. I probably need to learn Python one day for good..^^
I would like to achive at the end a list (.txt) like this:
curl -u john:connor -o NRO-08-029-Saint-Laurent.rar http://my-website.com/268441485/register/1348828088588073573
curl -u john:connor -o 08_001_059_DOE_CCR_CNV_SAD_001_B3.pdf http://my-website.com/268441485/register/1348828088574792249
curl -u john:connor -o 08_001_059_DOE_SRO_LST_LOD_001_A3.pdf http://my-website.com/268441485/register/1348828088574845667
Sorry if it is not quite clear, but my idea is like my code... really messy.
Thank you in advance for all your suggestions.
Cheers.

Tkinter Frame and griding

I am designing a front end using Tkinter but the .grid() option doesn't work as expected. I want to divide my screen to two section, LEFT and Right. All the buttons and labels on the left and the rest on the right.
# Initialise frames
self.frame_left = tk.Frame(root, bg = "pink", width = 100, height = 100).grid(row = 0, column = 0, sticky = 'E')
self.frame_right = tk.Frame(root, bg = "red", width = 100, height = 100).grid(row = 0, column = 1, sticky = 'W')
root.grid_columnconfigure(0, weight=0)
root.grid_columnconfigure(1, weight=0)
#labels
self.Name_Label = tk.Label(self.frame_left, text = "Name", font = 12, fg = 'green').grid(row = 1, column = 1)
self.Phone_Label = tk.Label(self.frame_left, text = "Phone Number", font = 12, fg= 'green').grid(row = 1, column =2)
self.University_label = tk.Label(self.frame_left, text = "University", font = 12, fg = 'green').grid(row = 1, column = 3)
#Search boxex
self.Name_box = tk.Entry(self.frame_left, width = 12, borderwidth = 2).grid(row = 2, column = 1)
self.Phone_box = tk.Entry(self.frame_left, width = 12, borderwidth = 2).grid(row = 2, column = 2)
self.Phone_box = tk.Entry(self.frame_left, width = 12, borderwidth = 2).grid(row = 2, column = 3)
# Buttons
self.Search_button = tk.Button(self.frame_left, text='Search', padx = 10, pady = 10, font = 12, fg = 'black').grid(row = 5, column = 1)
#Screen
#self.Screen = tk.Listbox(self.frame_right, width = 20, height = 20, font = 1).grid(row = 0, column = 0)
It seems the labels and buttons are being placed on the root window not the frames that has been defined.
Appreciate your help.
Regards
Khisrow
To divide your screen into 2 parts, you need to use the option columnconfigure:
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=1)
This creates a grid on the root window with 2 columns in which your self.frame_left and self.frame_right can be placed.
When you store a .grid() item you are not storing the widget:
self.frame_left = tk.Frame(root, bg = "pink", width = 100, height = 100).grid(row = 0, column = 0, sticky = 'E')
Stores the grid object to the variable, not the Frame.
This can be fixed by doing the creation and grid in two lines.
import tkinter as tk
class Test():
def __init__(self):
root = tk.Tk()
#EXAMPLE OF DIFFERENCE
self.frame_left = tk.Frame(root, bg = "pink", width = 100, height = 100).grid(row = 0, column = 0, sticky = 'E')
print(self.frame_left, "DIDN'T STORE THE FRAME")
self.frame_left = tk.Frame(root, bg = "pink", width = 100, height = 100)
self.frame_left.grid(row = 0, column = 0, sticky = 'E')
print(self.frame_left, "STORED THE FRAME")
#EXAMPLE OF DIFFERENCE
# Initialise frames
self.frame_left = tk.Frame(root, bg = "pink", width = 100, height = 100)
self.frame_left.grid(row = 0, column = 0, sticky = 'E')
self.frame_right = tk.Frame(root, bg = "red", width = 100, height = 100)
self.frame_right.grid(row = 0, column = 1, sticky = 'W')
root.grid_columnconfigure(0, weight=0)
root.grid_columnconfigure(1, weight=0)
#labels
self.Name_Label = tk.Label(self.frame_left, text = "Name", font = 12, fg = 'green').grid(row = 1, column = 1)
self.Phone_Label = tk.Label(self.frame_left, text = "Phone Number", font = 12, fg= 'green').grid(row = 1, column =2)
self.University_label = tk.Label(self.frame_left, text = "University", font = 12, fg = 'green').grid(row = 1, column = 3)
#Search boxex
self.Name_box = tk.Entry(self.frame_left, width = 12, borderwidth = 2)
self.Name_box.grid(row = 2, column = 1)
self.Phone_box = tk.Entry(self.frame_left, width = 12, borderwidth = 2)
self.Phone_box.grid(row = 2, column = 2)
self.Phone_box = tk.Entry(self.frame_left, width = 12, borderwidth = 2)
self.Phone_box.grid(row = 2, column = 3)
# Buttons
self.Search_button = tk.Button(self.frame_left, text='Search', padx = 10, pady = 10, font = 12, fg = 'black').grid(row = 5, column = 1)
#Screen
#self.Screen = tk.Listbox(self.frame_right, width = 20, height = 20, font = 1).grid(row = 0, column = 0)
Test()
EDIT: added a listbox with scrollbar
# Screen
scrollbar = tk.Scrollbar(self.frame_right)
scrollbar.grid(row = 0, column = 1, sticky = 'nsw')
self.Screen = tk.Listbox(self.frame_right, width = 20, height = 5, font = 1)
self.Screen.grid(row = 0, column = 0, sticky = 'e')
self.Screen.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=self.Screen.yview)

How to change background color in ttk python

I've made a simple gui age converter app but i want to change its bg color to black.
The problem is in the ttk frame.i don't know how to configure its bg color.
I have tried different methods but that didn't work.
i would be grateful if you guys could help.
here is the code
from tkinter import *
from tkinter import ttk
from PIL import Image
def calculate(*args):
try:
age_sec = int(age.get())
age_sec = age_sec * 12 * 365 * 24 * 60 * 60
age_seconds.set(age_sec)
except:
age_seconds.set('Either the field is empty or \n value is not numeric.')
root = Tk()
root.title("Converter")
root.configure(background="black")
mainframe = ttk.Frame(root, padding = "6 6 12 12")
mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S))
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)
#mainframe['borderwidth'] = 2
#mainframe['relief'] = 'groove'
age = StringVar()
age_seconds = StringVar()
ttk.Label(mainframe, foreground = "#4D4E4F", text = "Enter your Age: ").grid(column = 1, row = 1, sticky = E)
age_entry = ttk.Entry(mainframe, width = 30, textvariable = age)
age_entry.grid(column = 2, row = 1, sticky = (W, E))
ttk.Label(mainframe, foreground = "#4D4E4F", text = "your age in seconds is ").grid(column = 1, row = 2, sticky = (E))
ttk.Label(mainframe, textvariable = age_seconds, background = "lightyellow", foreground = "#727475", width = 30).grid(column = 2, row = 2, sticky = W)
#Mouse Events...\\
butt_image = PhotoImage(file = 'images.gif')
ttk.Button(mainframe, compound = TOP, text = "Hit Now", image =butt_image, cursor = "hand2", width= 30, command = calculate).grid(column = 2, row = 4, sticky = W)
l2 = ttk.Label(mainframe,foreground = "#4D4E4F", text = "Mouse Events: ").grid(column = 1, row = 3, sticky = E)
l = ttk.Label(mainframe,background = "lightyellow", foreground = "#727475", text = 'Measurement is starting...', width = 30)
l.grid(column = 2, row = 3, sticky = W)
l.bind('<Enter>', lambda e: l.configure(text = 'Moved Mouse Inside'))
l.bind('<Leave>', lambda e: l.configure(text = 'Mouse Moved Out'))
l.bind('<1>', lambda e: l.configure(text = 'left Mouse clicked'))
l.bind('<Double-1>', lambda e: l.configure(text = 'Double clicked'))
l.bind('<B1-Motion>', lambda e: l.configure(text = 'Left button drag to %d, %d' %(e.x, e.y)))
image = PhotoImage(file = 'waves.gif')
ttk.Label(mainframe, compound = CENTER, text = "Image text", font = ('roman', 9, 'normal'), foreground ='green', image = image).grid(column = 3, row = 1, sticky = (N, E))
#if '__name__' == '__main__':
for child in mainframe.winfo_children(): child.grid_configure(padx = 15, pady = 15)
age_entry.focus()
root.bind('<Return>', calculate)
root.mainloop()
A ttk.Frame does not have a background / bg option. To change it you need to use style.
See ttk.Frame Info
If you don't really need to use a ttk.Frame you can just switch to a tkinter.Frame

Odd grid sizing behaviour

I have been using tkinter's grid geometry manager and have configured all the rows and columns to weight = 1. The problem comes when I try to resize a Tk() window. When I resize it vertically only, the width of widgets and the proportion of column widths also change despite their equal weight. Furthermore, the width of the columns isn't in the desired proportions (i.e. all equal) before doing any resizing - to be precise the first column (with labels and buttons) is as wide as other two (holding a canvas with columnspan = 2 and two labels).
I think the problem is somehow connected to the PiSimulator._adjust_sizes function which dynamically changes the font or to the use of the canvas with a columnspan of 2.
Here's my code:
import tkinter as tk
from tkinter import font
class PiSimulator(tk.Frame):
def __init__(self, root, **kwargs):
super(PiSimulator, self).__init__(root, **kwargs)
self.root = root
self.root.title = 'Crtac krugova'
self.root.minsize(width = 600, height = 400)
self.pack(fill = 'both', expand = 1)
self.custom_font = font.Font(size = 20)
self.bind('<Configure>', self._adjust_sizes)
self.populate()
return
def populate(self):
tk.Label(self, text = 'Radijus:', font = self.custom_font, relief = 'raised',\
fg = 'green').grid(sticky='nswe', row = 0, column = 0)
self.radius = tk.IntVar()
tk.Spinbox(self, from_ = 1, to = 16, font = self.custom_font, textvariable = self.radius).grid(row = 1, column = 0)
tk.Label(self, text = 'Greška', font = self.custom_font, relief = 'raised', anchor='w',\
padx = 5, pady = 5, fg = 'green').grid(sticky='nswe', row = 0, column = 1)
self.error = tk.DoubleVar(0.0)
tk.Label(self, textvariable = self.error, font = self.custom_font, relief = 'raised',\
anchor='e', pady = 5, fg = 'green').grid(row = 0, column = 2, sticky='nswe')
self.canvas = PiCanvas(self, highlightthickness = 0)
self.canvas.grid(row = 1, rowspan = 5, column = 1, columnspan = 2, padx = 5, pady = 5, sticky='nswe')
tk.Button(self, text = 'Nacrtaj', font = self.custom_font, command = lambda: self.canvas.draw(self.radius.get())).grid(\
column = 0, row = 2, sticky='nswe')
tk.Button(self, text = 'Izbriši', font = self.custom_font, command = self.canvas.clear ).grid(\
column = 0, row = 3, sticky='nswe')
tk.Label(self, text = 'POVRŠINA', relief = 'groove', font = self.custom_font).grid(column = 0, row = 4, sticky='nswe')
self.area = tk.IntVar(0)
tk.Label(self, textvariable = self.area, relief = 'groove', font = self.custom_font).grid(column = 0, row = 5, sticky='nswe')
for i in range(3):
self.columnconfigure(i, weight = 1)
for i in range(6):
self.rowconfigure(i, weight = 1)
return
def _adjust_sizes(self, event):
new_font = int(event.height/15) - 2
self.custom_font.configure(size = new_font)
return
class PiCanvas(tk.Canvas):
def __init__(self, root, **kwargs):
super(PiCanvas, self).__init__(root, background = 'white', relief = 'groove', **kwargs)
self.root = root
self.cubes = []
self.circle = None
def draw(self, radius):
self.clear()
c_width = self.winfo_width()
c_height = self.winfo_height()
self.grid_side = int(min([c_width, c_height]))
self.grid_side-= 10
self.v_offset = (c_height - self.grid_side)/2
self.h_offset = (c_width - self.grid_side)/2
self.cube_side = self.grid_side/(2*radius)
vertix_coord = lambda x, x_cor, y_cor: x_cor*self.h_offset+y_cor*self.v_offset+x*self.cube_side
for i in range(2*radius):
new_line = []
for j in range(2*radius):
cube = self.create_rectangle(vertix_coord(i, 1, 0), vertix_coord(j, 0, 1),\
vertix_coord(i+1, 1, 0), vertix_coord(j+1, 0, 1), tag = 'unused')
new_line.append(cube)
self.cubes.append(new_line)
self.circle = self.create_oval(vertix_coord(0,1,0), vertix_coord(0,0,1),\
vertix_coord(2*radius,1,0), vertix_coord(2*radius,0,1),width = 2)
self.color_border_cubes()
print(self.circle)
def color_border_cubes(self):
circle_coords = self.coords(self.circle)
print(circle_coords)
for i in self.cubes:
for j in i:
cube_coords = self.coords(j)
if self.circle in self.find_overlapping(*cube_coords) + self.find_enclosed(*circle_coords):
self.itemconfigure(j, fill = 'green')
def clear(self):
self.cubes = []
self.circle = None
self.delete('all')
return
root = tk.Tk()
a=PiSimulator(root)
root.mainloop()

Resources