I am making a cube timer, and I want to make a table where you can see all of your times, including its scramble, date and other information using the text widget. If you insert a couple of times, it works fine, but when you do more than 50, you can't see some of the times. So, i'm using a scrollbar on a Canvas. I'm trying to make the canvas scroll down when you use the Scrollbar, but it doesn't seem to work. Here's my code.
class TimeTable(tk.Canvas):
"""Creates a time table using a tk.Canvas"""
def __init__(self, times, *args, **kwargs):
"""
:param times: list[CubeUtils.Time]
"""
# Initialize super class and define attributes
super().__init__(*args, **kwargs)
self.TIME_ATTRS = ["Time", "Scramble", "Date", "DNF"]
self.ScrollbarY = None
self.fullscreen = False
self.times = times
self.frame = self.nametowidget(self.winfo_parent())
self.parent = self.frame.nametowidget(self.frame)
self.config(height=len(self.times)*15)
self.frame.config(height=len(self.times)*15)
# Insert time attributes in entries
for column, attr in enumerate(self.TIME_ATTRS):
text = tk.Text(self, font=("Arial", 15, "bold"), width=50, height=1)
text.insert("0.0", attr)
text.config(state=tk.DISABLED)
text.grid(row=0, column=column+1, sticky=tk.E)
# Insert times
for time_count, time in enumerate(self.times):
time_info = [time_count+1, time.time, time.scramble, time.date, time.DNF]
if time_info[0] > 49 and self.ScrollbarY is None:
self.add_scrollbar()
time_info_font = ("Arial", 15, "bold")
for column, info in enumerate(time_info):
if isinstance(info, int) and not isinstance(info, bool):
text = tk.Text(self, font=time_info_font, width=2 if len(str(info)) <= 2 else len(str(info)), height=1, fg="#ff5000")
else:
text = tk.Text(self, font=time_info_font, width=50, height=1, fg="#ff5000")
text.insert("0.0", str(info))
text.grid(row=time_count + 1, column=column, sticky=tk.E)
text.config(state=tk.DISABLED)
# Bindings
self.parent.bind("<F11>", lambda key: self.toggle_fullscreen())
self.parent.bind("<Escape>", lambda key: self.exit_fullscreen())
self.parent.bind_all("<MouseWheel>", self.on_mousewheel)
Scrolling
def on_mousewheel(self, event):
"""Scrolls the canvas using the mouse wheel"""
self.yview_scroll(int(-1 * (event.delta / 120)), "units")
def add_scrollbar(self):
"""Adds a scrollbar to the frame"""
self.config(scrollregion=self.bbox("all"))
self.ScrollbarY = tk.Scrollbar(self.frame)
self.config(yscrollcommand=self.ScrollbarY.set)
self.ScrollbarY.config(command=self.yview)
self.ScrollbarY.pack(side=tk.RIGHT, expand=True, fill=tk.BOTH)
Fullscreening
def toggle_fullscreen(self):
"""Toggles fullscreen on and off"""
self.fullscreen = not self.fullscreen
self.parent.attributes("-fullscreen", self.fullscreen)
def exit_fullscreen(self):
"""Exists fullscreen"""
self.fullscreen = False
self.parent.attributes("-fullscreen", self.fullscreen)
By the way, the times argument must be a list with elements of type Time, here is the code for that class.
class Time:
"""Creates a time object that stores its time, scramble, date and whether or not it is a DNF"""
def __init__(self, time, scramble, date, DNF=False):
"""
:param time: float
:param scramble: str
:param date: datetime.datetime
:param DNF: bool
"""
if not isinstance(date, datetime.datetime):
raise TypeError("date parameter must be of type datetime.datetime")
if not isinstance(time, float):
raise TypeError("time parameter must be of type float")
if not isinstance(scramble, str):
raise TypeError("scramble parameter must be of type str")
if not isinstance(DNF, bool):
raise TypeError("DNF parameter must be of type bool")
self.time = time
self.scramble = scramble
self.date = datetime.datetime.strftime(date, "%Y-%m-%d-%I:%M %p")
self.DNF = DNF
I have tried reading web pages on effbot.com and other websites about putting a scrollbar on a canvas, but none of them are working for me.
Edit: Thanks to acw1668 I have finished my TimeTable class, here is the finished code for that
class TimeTable(tk.Frame):
"""Creates a time table using a tk.Canvas"""
def __init__(self, parent, times, *args, **kwargs):
"""
:param parent: tk.Tk()
:param times: list[CubeUtils.Time]
"""
# Initialize super class and define attributes
super().__init__(*args, **kwargs)
self.TIME_ATTRS = ["Time", "Scramble", "Date", "DNF"]
self.fullscreen = False
self.times = times
self.canvas = tk.Canvas(self)
self.frame = tk.Frame(self.canvas)
self.parent = parent
self.canvas.pack(fill=tk.BOTH, expand=True)
self.canvas.create_window(2, 2, window=self.frame, anchor="nw")
self.frame.bind("<Configure>", self.on_config)
self.parent.bind("<MouseWheel>", self.on_mousewheel)
self.parent.bind("<F11>", lambda event: self.toggle_fullscreen())
self.parent.bind("<Escape>", lambda event: self.exit_fullscreen())
self.populate()
def populate(self):
"""Populates the frame with the times"""
# Insert time attributes in text widgets
for column, attr in enumerate(self.TIME_ATTRS):
text = tk.Text(self.frame, font=("Arial", 15, "bold"), width=50, height=1)
text.insert("0.0", attr)
text.config(state=tk.DISABLED)
column += 4
text.grid(row=0, column=column)
# Insert times in text widgets
for time_count, time in enumerate(self.times):
time_info = [time_count + 1, time.time, time.scramble, time.date, time.DNF]
time_info_font = ("Arial", 15, "bold")
for column, info in enumerate(time_info):
if isinstance(info, int) and not isinstance(info, bool):
text = tk.Text(self.frame, font=time_info_font, width=2 if len(str(info)) <= 2 else len(str(info)),
height=1, fg="#ff5000")
else:
text = tk.Text(self.frame, font=time_info_font, width=50, height=1, fg="#ff5000")
text.insert("0.0", str(info))
row = time_count + 1
column += 3
text.grid(row=row, column=column, sticky=tk.E)
text.config(state=tk.DISABLED)
def on_config(self, event):
"""Reset the scroll region to encompass the inner frame"""
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
def on_mousewheel(self, event):
"""Scrolls the canvas using the mouse wheel"""
self.canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
def toggle_fullscreen(self):
"""Toggles fullscreen on and off"""
self.fullscreen = not self.fullscreen
self.parent.attributes("-fullscreen", self.fullscreen)
def exit_fullscreen(self):
"""Exists fullscreen"""
self.fullscreen = False
self.parent.attributes("-fullscreen", self.fullscreen)
Related
I am writing a Python GUI application with Tkinter where I have several frames that I want to manage separately. I want to put these "child" frames in separate classes (and ultimately in different files) to make the overall code more manageable. Each child class is basically a Tkinter frame with input elements. Based on selections from the main GUI, the relevant child class frame is shown. This is achieved using container and tkraise(). I want to reach child class variables from the main class but I cannot with my current code which is given below. I believe there is a problem with the initialization of child classes and/or the inheritance scheme of my app.
What is the correct way to structure a Python application in a setting where you have child classes being shown with container and tkraise() scheme and you want to reach child class variables form the main class? I appreciate your help.
import tkinter as tk
from tkinter import ttk
import math
# Padding values.
tab_padx = (10, 0)
tab_pady = (20, 0)
# Font settings.
font_1 = ("Arial", 13, "bold")
# Main class.
class Main_GUI(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.title("DEMO")
self.frame_blue_circle = BlueCircle(self, self)
self.frame_green_square = GreenSquare(self, self)
# Available shapes.
self.available_shapes = ["CIRCLE", "SQUARE"]
# Available colors.
self.available_colors = ["BLUE", "GREEN"]
# Function to run when color is changed.
def color_change(*args):
self.color = self.option_var_color.get()
if self.color == "BLUE" and self.shape == "CIRCLE":
self.type = "BlueCircle"
self.show_frame("BlueCircle")
elif self.color == "GREEN" and self.shape == "SQUARE":
self.show_frame("GreenSquare")
else:
self.show_frame("Unimplemented")
print(f"{self.color} {self.shape}")
# Function to run when shape is changed.
def shape_change(*args):
self.shape = self.option_var_shape.get()
if self.color == "BLUE" and self.shape == "CIRCLE":
self.show_frame("BlueCircle")
elif self.color == "GREEN" and self.shape == "SQUARE":
self.show_frame("GreenSquare")
else:
self.show_frame("Unimplemented")
print(f"{self.color} {self.shape}")
#GUI tabs
self.nb = ttk.Notebook(self)
self.nb.grid(row=1, column=0, sticky="w", padx=10, pady=10)
#GUI tab1 - Type selection.
self.tab1 = tk.Frame(self.nb)
self.nb.add(self.tab1, text="Type")
#GUI tab2 - Unput for selected type.
self.tab2 = tk.Frame(self.nb)
self.nb.add(self.tab2, text="Input")
#GUI tab3 - Calculate result for selected type with its specific inputs.
self.tab3 = tk.Frame(self.nb)
self.nb.add(self.tab3, text="Result")
# Tab-1 types.
# Shapes.
self.Label_shape = tk.Label(self.tab1, text = "Shape: ", font=font_1)
self.Label_shape.grid(row=10, column=0, padx=tab_padx, pady=tab_pady, sticky="W")
# Setup variable for disk type dropdown menu.
self.option_var_shape= tk.StringVar()
self.option_var_shape.set(self.available_shapes[0])
self.option_var_shape.trace("w", shape_change)
self.shape = self.option_var_shape.get()
self.shape_dropdown_menu = tk.OptionMenu(self.tab1, self.option_var_shape, *self.available_shapes)
self.shape_dropdown_menu.grid(row=10, column=1, sticky="WE", padx=tab_padx, pady=tab_pady)
self.shape_dropdown_menu.config(font=font_1, width=20)
self.shape_dropdown_menu["menu"].config(font=font_1)
# Colors.
self.Label_color = tk.Label(self.tab1, text = "Color: ", font=font_1)
self.Label_color.grid(row=20, column=0, padx=tab_padx, pady=tab_pady, sticky="W")
# Setup variable for disk type dropdown menu.
self.option_var_color= tk.StringVar()
self.option_var_color.set(self.available_colors[0])
self.option_var_color.trace("w", color_change)
self.color = self.option_var_color.get()
self.color_dropdown_menu = tk.OptionMenu(self.tab1, self.option_var_color, *self.available_colors)
self.color_dropdown_menu.grid(row=20, column=1, sticky="WE", padx=tab_padx, pady=tab_pady)
self.color_dropdown_menu.config(font=font_1, width=20)
self.color_dropdown_menu["menu"].config(font=font_1)
# Tab-2. Show frame based on selection in Tab-1.
# Container for frames.
container = tk.Frame(self.tab2)
container.grid(row=0, column=0)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (BlueCircle, GreenSquare, Unimplemented):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("BlueCircle")
# Tab-3. Calculate and display result based on Tab-1 and Tab-2.
# Label to display result.
result_text = "Result will be displayed here."
self.Label_result = tk.Label(self.tab3, text = result_text, font=font_1, fg="RED")
self.Label_result.grid(row=10, column=0, padx=tab_padx, pady=tab_pady, sticky="W")
self.button = tk.Button(self.tab3, text=f"Print", command=self.print_info)
self.button.grid(row=20, column=0, sticky="W")
# print(self.Label_result)
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
def print_info(self):
bc_text = f"Blue circle radius: {self.frame_blue_circle.radius}"
print(bc_text)
# Class defining GUI for BlueCircle.
class BlueCircle(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
super().__init__(*args, **kwargs)
self.parent = parent
self.radius = 0
# Function to run when rim radius is changed.
def Entry_change(*args):
value = self.Entry_var_radius.get()
if value == "":
self.Entry_var_radius.set(".0")
else:
try:
self.radius = float(value)
print(self.radius)
except ValueError:
self.Entry_var_radius.set("")
print(f"Warning! Floating point number only!")
tk.Frame.__init__(self, parent)
self.controller = controller
self.label = tk.Label(self, text="Blue Circle", font=font_1, fg="BLUE")
self.label.grid(row=0, column=0)
self.label = tk.Label(self, text="Radius:")
self.label.grid(row=1, column=0)
# Setup variable for entry to use in callback trace.
self.Entry_var_radius = tk.StringVar()
self.Entry_var_radius.trace("w", lambda name, index, mode, sv=self.Entry_var_radius: Entry_change(self.Entry_var_radius))
# Entry.
self.Entry_radius = tk.Entry(self, font=font_1, textvariable=self.Entry_var_radius)
self.Entry_radius.grid(row=1, column=1)
self.radius = self.Entry_radius.get()
# Class defining GUI for GreenSquare.
class GreenSquare(tk.Frame):
def __init__(self, parent, controller):
super().__init__()
self.parent = parent
# Function to run when rim radius is changed.
def Entry_change(*args):
value = self.Entry_var_lenght.get()
if value == "":
self.Entry_var_lenght.set(".0")
else:
try:
self.lenght = float(value)
self.green_square_area = self.lenght**2
# print(f"Side lenght: {self.lenght}. Area: {self.green_square_area:.2f}")
except ValueError:
self.Entry_var_lenght.set("")
print(f"Warning! Floating point number only!")
# Inıtialize variable.
self.green_square_area = 0
tk.Frame.__init__(self, parent)
self.controller = controller
self.label = tk.Label(self, text="Green Squire", font=font_1, fg="GREEN")
self.label.grid(row=0, column=0)
self.label = tk.Label(self, text="Side lenght:")
self.label.grid(row=1, column=0)
# Setup variable for entry to use in callback trace.
self.Entry_var_lenght = tk.StringVar()
self.Entry_var_lenght.trace("w", lambda name, index, mode, sv=self.Entry_var_lenght: Entry_change(self.Entry_var_lenght))
# Entry.
self.lenght = tk.Entry(self, font=font_1, textvariable=self.Entry_var_lenght)
self.lenght.grid(row=1, column=1)
self.lenght = self.Entry_var_lenght.get()
# Class defining GUI for unimplemented options.
class Unimplemented(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.label = tk.Label(self, text="To be implemented...", font=font_1, fg="RED")
self.label.grid(row=0, column=0)
if __name__ == "__main__":
app = Main_GUI()
app.mainloop()
Note that self.frame_blue_circle is not the instance of BlueCircle shown inside the notebook, so self.frame_blue_circle.radius is not the one input inside the "Input" tab.
The correct instance should be self.frames['BlueCircle'], so you need to use self.frames['BlueCircle'].radius instead:
def print_info(self):
bc_text = f"Blue circle radius: {self.frames['BlueCircle'].radius}"
print(bc_text)
In your __init__() method you have: self.frame_blue_circle = BlueCircle(self, self). This means that anywhere in your Main_GUI class (I mean typically other methods) you can access this and then its radius attribute.
In fact, you already do this here:
def print_info(self):
bc_text = f"Blue circle radius: {self.frame_blue_circle.radius}"
print(bc_text)
I have a question. I have this code:
import tkinter as tk
class new_f:
def __init__(self,root,num):
self.new_frame=tk.Frame(root,width=100,height=100,bg='white',bd=3,relief=tk.GROOVE)
self.new_frame.pack(side=tk.LEFT,fill=tk.X,expand=True)
self.num=num
def add_label(self,t):
self.l1=tk.Label(self.new_frame,bg='white',text=t)
self.l1.pack()
def return_instance(self):
return self.num
class Main_win:
def __init__(self,root):
self.root=root
self.bind_number=0
self.current_index=0
self.instance_list=[]
self.b1=tk.Button(self.root,text='Add Frame',command=self.add_frame_win)
self.b1.pack(side=tk.BOTTOM)
self.b2=tk.Button(self.root,text='Add text',command=self.add_text_frame)
self.b2.pack(side=tk.BOTTOM)
def return_instance_num(self,num,*args):
self.current_index=num
def add_frame_win(self):
new_in=new_f(self.root,self.bind_number)
self.instance_list.append(new_in)
new_in.new_frame.bind('<Button-1>',lambda evnt: self.return_instance_num(new_in.return_instance()))
#self.current_index=new_in.return_instance()
self.bind_number+=1
def add_text_frame(self):
instance=self.instance_list[self.current_index]
instance.add_label('Hello World')
root=tk.Tk()
ob=Main_win(root)
root.mainloop()
What I a trying to achieve is that I want to detect on which frame was the left mouse-button clicked so as to make that Frame active and add the labels to that particular Frame. However, I am stuck on how would I go about writing the code. I need a new class Because I don't know how many frames will the user need.
This is a short example of the code I will be implementing later. So my question is:
How will I go to detect which frame was picked so as to make it active to add the labels?
In this approach I have label l1 bound to Button-1
This was achieved by passing self to new_f instead of root
and binding self.l1 to Button-1
import tkinter as tk
class new_f:
def __init__(self, prog, num):
self.prog = prog
self.new_frame = tk.Frame(prog.root, width = 100, height = 100, bg = 'white', bd = 3, relief = tk.GROOVE)
self.new_frame.pack(side = tk.LEFT, fill = tk.X, expand = True)
self.num = num
def add_label(self, t):
self.l1 = tk.Label(self.new_frame, bg = 'white', text = t)
self.l1.pack()
# binding button-1 press to label
self.l1.bind("<Button-1>", lambda evnt: self.prog.return_instance_num(self.return_instance()))
def return_instance(self):
return self.num
class Main_win:
def __init__(self, root):
self.root = root
self.bind_number = 0
self.current_index = 0
self.instance_list = []
self.b1 = tk.Button(self.root, text = 'Add Frame', command = self.add_frame_win)
self.b1.pack(side = tk.BOTTOM)
self.b2 = tk.Button(self.root, text = 'Add text', command = self.add_text_frame)
self.b2.pack(side = tk.BOTTOM)
def return_instance_num(self, num, *args):
self.current_index = num
def add_frame_win(self):
# note passing self not root
new_in = new_f(self, self.bind_number)
self.instance_list.append(new_in)
new_in.new_frame.bind('<Button-1>', lambda evnt: self.return_instance_num(new_in.return_instance()))
#self.current_index = new_in.return_instance()
self.bind_number = self.bind_number + 1
def add_text_frame(self):
instance = self.instance_list[self.current_index]
instance.add_label('Hello World')
root = tk.Tk()
ob = Main_win(root)
# This necessary to prevent error if user hits 'Add text' before 'Add Frame'
ob.add_frame_win()
root.mainloop()
Here is an alternative method that uses dictionaries to store l1 and new_frame objects as keys and new_f instances as values.
This method can be used for other tkinter objects (Entry, Listbox, Text, Canvas)
import tkinter as tk
class new_f:
def __init__(self, parent):
self.parent = parent
self.frame = tk.Frame(
parent.root, width = 100, height = 100,
bg = "white", bd = 3, relief = tk.GROOVE)
self.frame.pack(
side = tk.LEFT, fill = tk.X, expand = True)
self.frame.bind("<Button-1>", parent.get_current_frame)
def add_label(self, t):
self.label = tk.Label(self.frame, bg = "white", text = t)
self.label.pack(fill = tk.BOTH, expand = True)
# bind button-1 to label, set instance_label and current to self
self.label.bind("<Button-1>", self.parent.get_current_label)
self.parent.instance_label[self.label] = self.parent.current = self
class Main_win:
instance_label = dict() # This method can be expanded for other objects
instance_frame = dict() # that you may want to create in frames
def __init__(self, root):
self.root = root
self.b1 = tk.Button(
self.root, text = "Add Frame", command = self.add_frame_win)
self.b1.pack(side = tk.BOTTOM)
self.b2 = tk.Button(
self.root, text = "Add text", command = self.add_text_frame)
self.b2.pack(side = tk.BOTTOM)
def get_current_label(self, ev):
self.current = self.instance_label[ev.widget]
def get_current_frame(self, ev):
self.current = self.instance_frame[ev.widget]
def add_frame_win(self):
# note passing self not root
self.new_in = new_f(self)
self.instance_frame[self.new_in.frame] = self.current = self.new_in
def add_text_frame(self):
# Change message with entry tool?
self.current.add_label("Hello World")
root = tk.Tk()
ob = Main_win(root)
# This necessary to prevent error if user hits 'Add text' before 'Add Frame'
ob.add_frame_win()
root.mainloop()
I have run into a problem with Tkinter and I cannot figure it out with hours of Googling. I found a large chunk of code which creates a calendar in Tkinter and returns the date in a certain format. When I use another class to try and access that data, it returns either errors, the location of where the text is stored, or just not what I need at all.
Here is the baseline code which creates a tkinter calendar application (credit to Rambarun Komaljeet)
import calendar
import tkinter as tk
import time
from tkinter import ttk
import sys
class MyDatePicker(tk.Toplevel):
"""
Description:
A tkinter GUI date picker.
"""
def __init__(self, widget=None, format_str=None):
"""
:param widget: widget of parent instance.
:param format_str: print format in which to display date.
:type format_str: string
Example::
a = MyDatePicker(self, widget=self.parent widget,
format_str='%02d-%s-%s')
"""
super().__init__()
self.widget = widget
self.str_format = format_str
self.title("Date Picker")
self.resizable(0, 0)
self.geometry("+630+390")
self.init_frames()
self.init_needed_vars()
self.init_month_year_labels()
self.init_buttons()
self.space_between_widgets()
self.fill_days()
self.make_calendar()
def init_frames(self):
self.frame1 = tk.Frame(self)
self.frame1.pack()
self.frame_days = tk.Frame(self)
self.frame_days.pack()
def init_needed_vars(self):
self.month_names = tuple(calendar.month_name)
self.day_names = tuple(calendar.day_abbr)
self.year = time.strftime("%Y")
self.month = time.strftime("%B")
def init_month_year_labels(self):
self.year_str_var = tk.StringVar()
self.month_str_var = tk.StringVar()
self.year_str_var.set(self.year)
self.year_lbl = tk.Label(self.frame1, textvariable=self.year_str_var,
width=3)
self.year_lbl.grid(row=0, column=5)
self.month_str_var.set(self.month)
self.month_lbl = tk.Label(self.frame1, textvariable=self.month_str_var,
width=8)
self.month_lbl.grid(row=0, column=1)
def init_buttons(self):
self.left_yr = ttk.Button(self.frame1, text="←", width=5,
command=self.prev_year)
self.left_yr.grid(row=0, column=4)
self.right_yr = ttk.Button(self.frame1, text="→", width=5,
command=self.next_year)
self.right_yr.grid(row=0, column=6)
self.left_mon = ttk.Button(self.frame1, text="←", width=5,
command=self.prev_month)
self.left_mon.grid(row=0, column=0)
self.right_mon = ttk.Button(self.frame1, text="→", width=5,
command=self.next_month)
self.right_mon.grid(row=0, column=2)
def space_between_widgets(self):
self.frame1.grid_columnconfigure(3, minsize=40)
def prev_year(self):
self.prev_yr = int(self.year_str_var.get()) - 1
self.year_str_var.set(self.prev_yr)
self.make_calendar()
def next_year(self):
self.next_yr = int(self.year_str_var.get()) + 1
self.year_str_var.set(self.next_yr)
self.make_calendar()
def prev_month(self):
index_current_month = self.month_names.index(self.month_str_var.get())
index_prev_month = index_current_month - 1
# index 0 is empty string, use index 12 instead,
# which is index of December.
if index_prev_month == 0:
self.month_str_var.set(self.month_names[12])
else:
self.month_str_var.set(self.month_names[index_current_month - 1])
self.make_calendar()
def next_month(self):
index_current_month = self.month_names.index(self.month_str_var.get())
try:
self.month_str_var.set(self.month_names[index_current_month + 1])
except IndexError:
# index 13 does not exist, use index 1 instead, which is January.
self.month_str_var.set(self.month_names[1])
self.make_calendar()
def fill_days(self):
col = 0
# Creates days label
for day in self.day_names:
self.lbl_day = tk.Label(self.frame_days, text=day)
self.lbl_day.grid(row=0, column=col)
col += 1
def make_calendar(self):
# Delete date buttons if already present.
# Each button must have its own instance attribute for this to work.
try:
for dates in self.m_cal:
for date in dates:
if date == 0:
continue
self.delete_buttons(date)
except AttributeError:
pass
year = int(self.year_str_var.get())
month = self.month_names.index(self.month_str_var.get())
self.m_cal = calendar.monthcalendar(year, month)
# build dates buttons.
for dates in self.m_cal:
row = self.m_cal.index(dates) + 1
for date in dates:
col = dates.index(date)
if date == 0:
continue
self.make_button(str(date), str(row), str(col))
def make_button(self, date, row, column):
"""
Description:
Build a date button.
:param date: date.
:type date: string
:param row: row number.
:type row: string
:param column: column number.
:type column: string
"""
exec(
"self.btn_" + date + " = ttk.Button(self.frame_days, text=" + date
+ ", width=5)\n"
"self.btn_" + date + ".grid(row=" + row + " , column=" + column
+ ")\n"
"self.btn_" + date + ".bind(\"<Button-1>\", self.get_date)"
)
def delete_buttons(self, date):
"""
Description:
Delete a date button.
:param date: date.
:type: string
"""
exec(
"self.btn_" + str(date) + ".destroy()"
)
def get_date(self, clicked=None):
"""
Description:
Get the date from the calendar on button click.
:param clicked: button clicked event.
:type clicked: tkinter event
"""
clicked_button = clicked.widget
year = self.year_str_var.get()
month = self.month_str_var.get()
date = clicked_button['text']
self.full_date = self.str_format % (date, month, year)
print(self.full_date)
sys.stderr.write(self.full_date)
# Replace with parent 'widget' of your choice.
try:
self.widget.delete(0, tk.END)
self.widget.insert(0, self.full_date)
except AttributeError:
pass
if __name__ == '__main__':
def application():
MyDatePicker(format_str='%02d-%s-%s')
root = tk.Tk()
btn = tk.Button(root, text="test", command=application)
btn.pack()
root.mainloop()
What I've been trying to do is get that data back into the first tkinter box that I open so I can use it for a search feature. It seems that the actual stuff I need is in the get_date() section where it pulls information and prints out the date in the console as the variable self.full_date.
How do I get this to work within a different class? This is what I have so far (the MyDatePicker is also there but I didn't want to clutter too much space)
class Main_Window(tk.Frame,MyDatePicker):
def __init__(self, root):
self.root=root
#self.MyDatePicker=MyDatePicker()
tk.Frame.__init__(self, root)
self.root.geometry('300x300')
b1 = tk.Button(self.root, text="Add another window", command =lambda: self.newWindow(1))
b1.grid(column=1, row=1)
self.total = 0
self.count=0
self.total_label_text = tk.IntVar()
self.total_label_text.set(self.total)
#self.total_label_text
self.lbl1=tk.Label(self.root,textvariable=self.total_label_text)
self.lbl1.grid(column=1,row=2)
b2 = tk.Button(self.root, text="Add another window", command =lambda: self.newWindow(2))
b2.grid(column=2, row=1)
self.total2 = 0
self.count2=0
self.total_label_text2 = tk.IntVar()
self.total_label_text2.set(self.total2)
#self.total_label_text
self.lbl2=tk.Label(self.root,textvariable=self.total_label_text2)
self.lbl2.grid(column=2,row=2)
b1 = tk.Button(self.root, text="Add another window", command =lambda: self.press_calendar(format_str='%02d-%s-%s',Class=MyDatePicker))
b1.grid(column=1, row=1)
def press_calendar(self,format_str,Class):
self.MyDatePicker1=Class(format_str)
self.MyDatePicker1.get_date(self)
def newWindow(self,m):
if m ==1:
self.count += 1
if m ==2:
self.count2+=1
self.window = tk.Toplevel(self)
but1=tk.Button(self.window,text="Add One",command =lambda:self.close(m))
but1.grid(row=2,column=1)
def close(self,m):
if m==1:
self.total_label_text.set(self.count)
if m==2:
self.total_label_text2.set(self.count2)
self.window.destroy()
if __name__ == "__main__":
root = tk.Tk()
Main_Window(root)
root.mainloop()
I'm trying to make this happen in the press_calendar part of my code where I call the other class, run it, and have it return the date.
All I'm getting is the variable location or I have to initialize all the variables from the MyDatePicker in the Main_Window class.
What can I do better to actually retrieve the date and use it in my main window?
Thank you for the help!
As far as I can see, the MyDatePicker() class is not intended to be inherited, but used as a server. In my example I'm calling MyDatePicker() and providing the widget of master where I'd like the date string to be sent.
class Main_Window(tk.Frame):
def __init__(self, root):
self.root = root
self.root.geometry('300x300')
tk.Frame.__init__(self, root)
self.pack()
b1 = tk.Button(self, text="Pick date", command=self.pick_date)
b1.grid(column=0, row=0)
# Widget to recieve date string from MyDatePicker
self.date_display = tk.Entry(self, text='apa', width=20)
self.date_display.grid(column=0, row=1)
def pick_date(self):
# Call MyDatePicker and provide widget to recieve date string
picker = MyDatePicker(widget=self.date_display, format_str='%02d-%s-%s')
if __name__ == "__main__":
root = tk.Tk()
Main_Window(root)
root.mainloop()
You will have to remove the if __name__ == "__main__": section from the MyDatePicker() code for it to function if the are to be in the same file.
I'm creating a GUI that interacts with a Postgresql database.
The GUI displays all the contents of a certain table when the program launches.
I have a button programmed to add/remove entries.
The buttons work as when I checked the database, the entries are added to the database but I don't know how to refresh the TreeView in order for it to reflect the changes in the database.
My attempt to make it refresh is to clear (also to build) the TreeView is the _build_tree... code below:
def _build_tree(self):
for i in self.tree.get_children():
self.tree.delete(i)
for col in self.header:
self.tree.heading(col, text=col.title(),command=lambda c=col: self.sortby(self.tree, c, 0))
# adjust the column's width to the header string
self.tree.column(col,width=tkFont.Font().measure(col.title()))
for item in self.content:
self.tree.insert('', 'end', values=item)
# adjust column's width if necessary to fit each value
for ix, val in enumerate(item):
col_w = tkFont.Font().measure(val)
if self.tree.column(self.header[ix],width=None)<col_w:
self.tree.column(self.header[ix], width=col_w)
My complete code is below:
from tkinter import *
import tkinter.font as tkFont
import tkinter.ttk as ttk
import datetime
import psycopg2 as pg2
myFont = ('Impact',24)
myFontColor = 'red'
def check(): #to check if button works
print ("It works!")
class Database:
def __init__(self):
self.conn = pg2.connect(database='inventory', user='loremipsum', password='loremipsum')
self.cur = self.conn.cursor()
self.timeNow = datetime.datetime.now()
def view_rooms(self):
self.cur.execute('''
SELECT * FROM rooms
ORDER BY room_id;
''')
return self.cur.fetchall()
def add_room(self,roomID,roomName,floor):
addRoom = '''
INSERT INTO rooms (room_id,room_name,floor)
VALUES ({},'{}',{});
'''.format(roomID,roomName,floor)
self.cur.execute(addRoom)
self.conn.commit()
def del_room(self,roomID):
addRoom = '''
DELETE FROM rooms
WHERE room_id={};
'''.format(roomID)
self.cur.execute(addRoom)
self.conn.commit()
def __del__(self):
self.conn.close()
database = Database()
class Page(Frame):
def __init__(self, *args, **kwargs):
Frame.__init__(self, *args, **kwargs)
def show(self):
self.lift()
class RoomPage(Page):
def __init__(self, *args, **kwargs): #create widgets
Page.__init__(self, *args, **kwargs)
Label(self,text="ROOM",font=myFont,fg=myFontColor).grid(row=0,column=0, sticky=W)
Button(self,text="SEARCH",command=check).grid(row=0,column=1, sticky=W+E)
self.header = ['Room ID','Room','Floor']
self.content = database.view_rooms()
self.tree=ttk.Treeview(self,columns=self.header, show="headings")
vsb = ttk.Scrollbar(self,orient="vertical",command=self.tree.yview)
hsb = ttk.Scrollbar(self,orient="horizontal",command=self.tree.xview)
self.tree.configure(yscrollcommand=vsb.set,xscrollcommand=hsb.set)
self.tree.bind('<ButtonRelease-1>',self.get_selected_row)
self.tree.grid(column=0, row=1, columnspan=4, sticky='nsew')
vsb.grid(column=4, row=1, sticky='ns')
hsb.grid(column=0, row=2, columnspan=4, sticky='ew')
self._build_tree()
Button(self,text="ADD",command=AddRoom).grid(row=2,column=0,sticky=W+E)
Button(self,text="REMOVE",command=self.deleteRoom).grid(row=2,column=1, sticky=W+E)
Button(self,text="CLOSE",command=root.destroy).grid(row=2,column=3, sticky=W+E)
def _build_tree(self):
for i in self.tree.get_children():
self.tree.delete(i)
for col in self.header:
self.tree.heading(col, text=col.title(),command=lambda c=col: self.sortby(self.tree, c, 0))
# adjust the column's width to the header string
self.tree.column(col,width=tkFont.Font().measure(col.title()))
for item in self.content:
self.tree.insert('', 'end', values=item)
# adjust column's width if necessary to fit each value
for ix, val in enumerate(item):
col_w = tkFont.Font().measure(val)
if self.tree.column(self.header[ix],width=None)<col_w:
self.tree.column(self.header[ix], width=col_w)
def sortby(self,tree, col, descending):
"""sort tree contents when a column header is clicked on"""
# grab values to sort
data = [(tree.set(child, col), child) \
for child in tree.get_children('')]
# now sort the data in place
data.sort(reverse=descending)
for ix, item in enumerate(data):
tree.move(item[1], '', ix)
# switch the heading so it will sort in the opposite direction
tree.heading(col, command=lambda col=col: sortby(tree, col, \
int(not descending)))
def get_selected_row(self,event):
selection = self.tree.item(self.tree.selection())
self.selected_tuple=selection['values'][0]
def openRoom(self,event):
index=self.roomList.curselection()[0]
self.selected_tuple=self.roomList.get(index)[0]
print (self.selected_tuple)
def deleteRoom(self):
database.del_room(self.selected_tuple)
self.clear()
self.build()
class AddRoom:
def __init__(self, *args, **kwargs): #create widgets
window = Toplevel(root)
window.title("Room Details")
roomDetailsLabel = Label (window,text="Room Details",font=myFont,fg=myFontColor).grid(row=0,column=0, sticky=W,columnspan=2)
roomNumLabel = Label(window,text="Room ID").grid(row=1,column=0, sticky=E)
self.roomNumText = StringVar()
roomNameEntry = Entry(window,textvariable=self.roomNumText,width=30).grid(row=1,column=1,sticky=W)
roomNameLabel = Label(window,text="Room Name").grid(row=2,column=0, sticky=E)
self.roomNameText = StringVar()
roomNameEntry = Entry(window,textvariable=self.roomNameText,width=30).grid(row=2,column=1,sticky=W)
floorLabel = Label(window,text="Floor").grid(row=3,column=0, sticky=E)
self.floorText = StringVar()
floorEntry = Entry(window,textvariable=self.floorText,width=30).grid(row=3,column=1,sticky=W)
Button(window,text="SAVE",command=self.add_room).grid(row=4,column=0,sticky=W+E)
Button(window,text="CLOSE",command=window.destroy).grid(row=4,column=1,sticky=W+E)
def add_room(self):
database.add_room(self.roomNumText.get(),self.roomNameText.get(),self.floorText.get())
p1._build_tree()
class MainView(Frame):
def __init__(self, *args, **kwargs):
global p1
Frame.__init__(self, *args, **kwargs)
p1 = RoomPage(self)
buttonframe = Frame(self)
container = Frame(self)
buttonframe.pack(side="top", fill="x", expand=False)
container.pack(side="top", fill="both", expand=True)
p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
p1.show()
if __name__ == "__main__": #main loop
root = Tk()
root.title('Inventory System')
root.configure(bg="#BDE9EB")
parent = Frame(root, padx=10, pady=10)
parent.pack(fill=BOTH, expand=True)
main = MainView(parent)
main.pack(side="top", fill="both", expand=True)
root.wm_geometry("600x350")
root.mainloop()
How do you refresh the TreeView to reflect the changes made in the database?
Hello seems like the problem you have encountered is like mine.
You are correct when you call the get.children() after that what you will do is call the database again.
Here is my code, i hope you will get this.
def _build_tree(self):
cursor=db.cursor(buffered=True)
cursor.execute("SELECT ")
result = cursor.fetchall()
for i in self.tree.get_children():
self.tree.delete(i)
for i in result:
self.tree.insert('', 'end', values=(i[0],i[2]..)
I'm trying to create a modular class ( for some gui buttons ).
CoreButton should consist of most methods for a common button, including tk frame.
Goal is to inheret CoreButton - and to use its frame to build rest of button's GUI - it does not appear.
any help will be appriciated
class CoreButton(ttk.Frame):
def __init__(self, master,nickname, hw_in=[], hw_out=[],ip_in='', ip_out='', sched_vector=[]):
ttk.Frame.__init__(self, master)
self.master = master
if ip_in == '': ip_in = ip_out # in case remote input is not defined
self.grid()
#####Rest of code
and class that inherits:
class ToggleBut2(CoreButton):
def __init__(self, master, hw_in=[], hw_out=[],ip_in='', ip_out='', sched_vector=[]):
CoreButton.__init__(self, master, nickname="JOHM", hw_in=hw_in, hw_out=hw_out, ip_in=ip_in, ip_out=ip_out, sched_vector=sched_vector)
self.master = master
def build_gui(self, nickname='babe', height=3, width=13):
self.button = tk.Checkbutton(self, text=nickname, variable=self.but_var, indicatoron=0, height=height, width=width, command=self.sf_button_press)
self.button.grid(row=0, column=0)
I don't know what you try to do but I would do something like this
I don't use self.grid() inside class, so outside class I can use tb1.pack() or tb1.grid() depends on which layout manager I use in window.
In __init__ I execute self.build_gui() so I don't have to do it manually, but now all classes have to create self.build_gui() without arguments.
I add Label only for test - to display "selected"/"not selected". You don't need it.
import tkinter as tk
from tkinter import ttk
class CoreButton(ttk.Frame):
def __init__(self, master, nickname, hw_in=None, hw_out=None, ip_in=None, ip_out=None, sched_vector=None):
ttk.Frame.__init__(self, master)
self.nickname = nickname
self.hw_in = hw_in
if self.hw_in is None:
self.hw_in = []
#self.hw_in = hw_in or []
self.hw_out = hw_out
if self.hw_out is None:
self.hw_out = []
#self.hw_out = hw_out or []
self.ip_out = ip_out
self.ip_in = ip_in
if self.ip_in is None:
self.ip_in = self.ip_out # in case remote input is not defined
#self.ip_in = hw_in or self.ip_out
self.sched_vector = sched_vector
if sched_vector is None:
sched_vector = []
#self.sched_vector = sched_vector or []
self.build_gui() # <--- to build it automatically
def build_gui(self):
# you will overide it in child widgets
raise NotImplementedError('You have to override method build_gui()')
class ToggleBut2(CoreButton):
def __init__(self, master, hw_in=None, hw_out=None, ip_in=None, ip_out=None, sched_vector=None, height=3, width=13):
self.height = height
self.width = width
# `self.but_var` is used in `build_gui` so it has to be created before `__init__` which executes `build_gui`
# or create it directly in `build_gui`
#self.but_var = tk.StringVar()
CoreButton.__init__(self, master, "JOHM", hw_in, hw_out, ip_in, ip_out, sched_vector)
def build_gui(self, nickname='babe'):
self.but_var = tk.IntVar()
self.button = tk.Checkbutton(self, text=self.nickname, variable=self.but_var, indicatoron=0, height=self.height, width=self.width, command=self.sf_button_press)
self.button.grid(row=0, column=0)
self.label = tk.Label(self, text='[not selected]')
self.label.grid(row=1, column=0)
def sf_button_press(self):
print(self.but_var.get())
if self.but_var.get() == 0:
self.label['text'] = '[ not selected ]'
else:
self.label['text'] = '[ selected ]'
# --- main ---
root = tk.Tk()
tb1 = ToggleBut2(root, height=1, width=10)
tb1.pack()
tb2 = ToggleBut2(root, height=3, width=30)
tb2.pack()
tb2 = ToggleBut2(root, height=5, width=50)
tb2.pack()
root.mainloop()