subclass Label in tkinter - python

I need to subclass a Label widget, so that the browser can open a link while the mouse clicks on it.
Here is the code snippet of what I've done so far.
from tkinter import *
import webbrowser
class HyperLinkLabel(Label):
def __init__(self, link, *args, **kwargs):
Label.__init__(self, *args, **kwargs)
self.link = link
self.bind("<Button-1>", self.click_callback)
def click_callback(self):
webbrowser.open_new(self.link)
if __name__ == '__main__':
master = Tk()
root = Frame(master)
label1 = HyperLinkLabel(root, link='https://www.google.com')
label1.config(text='hello')
label1.pack()
root.master.minsize(100, 50)
root.mainloop()

You should declare master (or parent) parameter and pass it to Label constructor. (root from the perspect of caller)
event handle should have event parameter even though you don't use it. Otherwise TypeError exception is thrown.
Pack frame so that widgets inside it is visible.
from tkinter import *
import webbrowser
class HyperLinkLabel(Label):
def __init__(self, master, link, *args, **kwargs): # <-- pass master parameter
Label.__init__(self, master, *args, **kwargs) # <-- pass master parameter
self.link = link
self.bind("<Button-1>", self.click_callback)
def click_callback(self, event): # <--- missed event parameter
webbrowser.open_new(self.link)
if __name__ == '__main__':
master = Tk()
root = Frame(master)
label1 = HyperLinkLabel(root, link='https://www.google.com')
label1.config(text='hello')
label1.pack()
root.pack() # <-- should pack frame; otherwise link widget is not visible
root.master.minsize(100, 50)
root.mainloop()

Related

.delete is not working inside class in tkinter

I am trying to make a tkinter desktop application (Notepad) using classes but I found an Attribute Error in my code. I made three files "menu_option.py", "textarea.py" and "window_frame.py". Run the "menu_option.py" file so that you found the error. I am using python (3.9). Also is there any another way to connect "new_file" function to menu item.
Here is my code below:
menu_option.py
from tkinter import *
from textarea import TextArea
from window_frame import Window
class Menu_Option(TextArea):
file = None
def __init__(self, master, newfile=None):
super().__init__(root)
self.Menubar = 'MenuBar'
self.Filemenu = 'FileMenu'
self.root = self.root
self.master = self.master
self.newfile = newfile
def launch(self):
self.Menubar = Menu(self.root)
self.Filemenu = Menu(master=self.Menubar, tearoff=0)
self.Filemenu.add_command(label='New File...', command=self.newfile)
self.Menubar.add_cascade(label='File', menu=self.Filemenu)
self.root.config(menu=self.Menubar)
class Features(Menu_Option):
def __init__(self, master):
super().__init__(master, newfile=self.new_file)
def new_file(self):
global file
self.root.title(self.title)
file = None
self.textarea.delete(1.0, END)
if __name__ == '__main__':
root = Tk()
Window(root).launch()
TextArea(root).launch()
Menu_Option(root).launch()
Features(root).launch()
root.mainloop()
textarea.py
from tkinter import *
from window_frame import Window
from tkinter.scrolledtext import ScrolledText
class TextArea(Window):
def __init__(self, name):
super().__init__(name)
self.name = self.root
self.master = 'root'
self.textarea = 'text_area'
self.font = 'courier 14 normal'
def launch(self):
self.textarea = ScrolledText(self.root, font=self.font)
self.textarea.pack(expand=True, fill=BOTH)
if __name__ == '__main__':
root = Tk()
Window(root).launch()
TextArea(root).launch()
root.mainloop()
window_frame.py
from tkinter import *
class Window:
def __init__(self, root):
self.root = root
self.geometry = '1000x550+100+100'
self.title = 'Untitled - ProBook'
def launch(self):
self.root.geometry(self.geometry)
self.root.title(self.title)
if __name__ == '__main__':
root = Tk()
Window(root).launch()
root.mainloop()
Error:
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\Installed Programs\Python\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:\Users\vaish\OneDrive\ProBook\menu_option.py", line 35, in new_file
self.textarea.delete(1.0, END)
AttributeError: 'str' object has no attribute 'delete'
Since Menu_Option() has override launch() function, therefore TextArea.launch() will not be executed and so instance variable textarea is still a string.
If child class wants to inherit parent class launch() functionality, you need to call the parent class launch() in its launch() function:
textarea.py
class TextArea(Window):
...
def launch(self):
super().launch() # call parent class launch()
self.textarea = ScrolledText(self.root, font=self.font)
self.textarea.pack(expand=True, fill=BOTH)
menu_option.py
class Menu_Option(TextArea):
...
def launch(self):
super().launch() # execute parent class launch()
...
...
if __name__ == "__main__":
root = Tk()
#Window(root).launch() # <- no need to execute
#TextArea(root).launch() # <- no need to execute
#Menu_Option(root).launch() # <- no need to execute
Features(root).launch()
root.mainloop()
Note that Window(root).launch(), TextArea(root).launch() and Menu_Option(root).launch() are not required.
You have initialized self.textarea="text_area" in textarea.py . But when you import it in menu_option.py, you are overwriting the function launch, which is supposed to set the value of self.textarea to a ScrolledText and pack it. To solve this you have to include the code in launch function of textarea.py in the function launch of Menu_Option class.
from tkinter import *
from tkinter.scrolledtext import ScrolledText
from textarea import TextArea
from window_frame import Window
class Menu_Option(TextArea):
file = None
def __init__(self, master, newfile=None):
super().__init__(root)
self.Menubar = 'MenuBar'
self.Filemenu = 'FileMenu'
self.root = self.root
self.master = self.master
self.newfile = newfile
def launch(self):
self.Menubar = Menu(self.root)
#You have to include these 2 lines of code which were overwritten
self.textarea = ScrolledText(self.root, font=self.font)
self.textarea.pack(expand=True, fill=BOTH)
self.Filemenu = Menu(master=self.Menubar, tearoff=0)
self.Filemenu.add_command(label='New File...', command=self.newfile)
self.Menubar.add_cascade(label='File', menu=self.Filemenu)
self.root.config(menu=self.Menubar)

Tkinter passing callback function result to another class

I wonder how to pass return value from one class to another class in tkinter.
In my program I have DataChosenForm class where I want to choose option in Combobox and pass this result to another class ReturnData to set a variable in a Label.
import tkinter as tk
from tkinter import ttk
class DataChosenForm(tk.Frame):
def __init__(self, parent):
super().__init__(parent)
chosen = tk.LabelFrame(self, text="wybór")
chosen.grid(row=0)
self.combo = ttk.Combobox(chosen)
self.combo['values'] = ('wizz', 'ryanair', 'lot')
self.combo.grid(row=0, column=2, padx=80, pady=10)
self.combo.bind("<<ComboboxSelected>>", self.callback)
def callback(self, event=None):
if event.widget.get() == 'wizz':
print('wizz')
return 'wizz'
elif event.widget.get() == 'ryanair':
print('ryanair')
return 'ryanair'
elif event.widget.get() == 'lot':
print('lot')
return 'lot'
class ReturnData(tk.Frame):
def __init__(self, parent):
super().__init__(parent)
var = tk.StringVar()
message_box = tk.LabelFrame(self, text="wynik")
message_box.grid(row=1)
mb = tk.Label(message_box, textvariable=var,anchor='nw')
mb.pack(padx=120, pady=30)
class Application(tk.Tk):
def __init__(self):
super().__init__()
self.title("program do wyszukiwania cen lotów")
self.geometry('300x200')
self.resizable(width=False, height=False)
DataChosenForm(self).grid(row=0, column=0)
ReturnData(self).grid(row=1)
if __name__ == "__main__":
app = Application()
app.mainloop()
You could first display the combobox DataChosenForm(self).grid(row=0, column=0) without calling the ReturnData in the Application class.
Then, in the callback() method collect the choice choice = event.widget.get() and pass it to ReturnData. This would mean, however, that the LabelFrame is displayed only after a choice is made.
import tkinter as tk
from tkinter import ttk
class DataChosenForm(tk.Frame):
def __init__(self, parent):
super().__init__(parent)
chosen = tk.LabelFrame(self, text="wybór")
chosen.grid(row=0)
self.combo = ttk.Combobox(chosen)
self.combo['values'] = ('wizz', 'ryanair', 'lot')
self.combo.grid(row=0, column=2, padx=80, pady=10)
self.combo.bind("<<ComboboxSelected>>", self.callback)
def callback(self, event=None):
choice = event.widget.get()
print(choice)
ReturnData(self, choice).grid(row=1)
class ReturnData(tk.Frame):
def __init__(self, parent, choice):
super().__init__(parent)
message_box = tk.LabelFrame(self, text="wynik")
message_box.grid(row=1)
mb = tk.Label(message_box, text=choice, anchor='nw')
mb.pack(padx=120, pady=30)
class Application(tk.Tk):
def __init__(self):
super().__init__()
self.title("program do wyszukiwania cen lotów")
self.geometry('300x200')
self.resizable(width=False, height=False)
DataChosenForm(self).grid(row=0, column=0)
if __name__ == "__main__":
app = Application()
app.mainloop()
I think #Julia has the basically the right architecture for your tkinter application, but her answer could be improved by using a tkinter Variable — because all widgets associated with one will automatically update their displayed value whenever the Variable is changed (by one of them or something else).
Here's a little documentation on Variable Classes. Note that since ttk.Combobox with have all the methods of a tk.Entry widgets, here's a bit of documentation about them (which also happens to illustrate the "pattern" of using of a StringVar in conjunction with one so it also applies to a ttk.Comboboxs).
Generally, you can tell a widget to use a tkinter Variable by specifying an instance of one as the option textvariable= keyword argument when the widget is created. You can also set the option using the partial dictionary interface most widgets support, so assignments like widget['textvariable'] = variable are another way to make use of them — the code below makes use of both of these ways.
Here's Julia's code modified to use a tk.StringVar. Note the Combobox doesn't need a callback function to bind the <<ComboboxSelected>> event to it, so all that complexity has been eliminated.
import tkinter as tk
from tkinter import ttk
class DataChosenForm(tk.Frame):
def __init__(self, parent):
super().__init__(parent)
choice = tk.LabelFrame(self, text="wybór")
choice.grid(row=0)
self.combo = ttk.Combobox(choice)
self.combo['textvariable'] = parent.var # Use shared variable.
self.combo['values'] = ('wizzair', 'ryanair', 'lot')
self.combo.grid(row=0, column=2, padx=80, pady=10)
class ReturnData(tk.Frame):
def __init__(self, parent):
super().__init__(parent)
message_box = tk.LabelFrame(self, text="wynik")
message_box.grid(row=1)
mb = tk.Label(message_box, textvariable=parent.var, # Use shared variable.
anchor='nw', width=20)
mb.pack(padx=120, pady=30)
class Application(tk.Tk):
def __init__(self):
super().__init__()
self.title("program do wyszukiwania cen lotów")
self.geometry('300x200')
self.resizable(width=False, height=False)
self.var = tk.StringVar(value='Dokonać wyboru') # Create shared variable.
DataChosenForm(self).grid(row=0, column=0)
ReturnData(self).grid(row=1)
if __name__ == "__main__":
app = Application()
app.mainloop()
You can just pass other class or it's field to __init__ of the DataChosenForm, and to callback function from there, where then you can change class/field directly. Here's what I mean, but I use TreeView:
import tkinter as tk
from tkinter import ttk
class ConnectedClass:
def __init__(self):
self.received = "will be changed"
class TreeViewWrapper(ttk.Treeview):
def __init__(self, master, connected_class, **kw):
super().__init__(master, **kw)
parents = []
for i in range(10):
parent = self.insert("", "end", text="Item %s" % i, tags=str(i))
for i in range(3):
self.insert(parent, "end", text="Item %s" % i, tags=str(i))
self.bind("<Control-r>", lambda e: self.pass_to_other(e, connected_class))
def pass_to_other(self, _, connected_class):
items = self.selection()
connected_class.received = items
class App:
def __init__(self):
self.root = tk.Tk()
self.con_class = ConnectedClass()
self.tree = TreeViewWrapper(self.root,self.con_class)
self.tree.pack()
self.root.bind("<Control-p>",lambda e:print(self.con_class.received))
self.root.mainloop()
if __name__ == "__main__":
app = App()

Tkinter in python3, menubar does not working

Well, i want to add menubar, but something is going wrong.
It says: AttributeError: 'NoneType' object has no attribute 'config'
My code:
from tkinter import *
class ApplicationWindow(Tk):
def __init__(self, master=None):
Tk.__init__(self, master)
self.master = master
self.geometry('800x400')
self.f_app = Frame(self).pack()
menubar = Menu(self.master)
self.master.config(menu=menubar)
fileMenu = Menu(menubar)
fileMenu.add_command(label="Exit", command=self.onExit)
menubar.add_cascade(label="File", menu=fileMenu)
self.b_log = Button(self, width=10, text="Войти", command=self.func).pack()
def onExit(self):
self.quit()
def func(self):
print("hello")
def main():
# root = tk
app = ApplicationWindow()
app.mainloop()
if __name__ == '__main__':
main()
You're initializing your ApplicationWindow class without passing any arguments in, like this app = ApplicationWindow(). In your init method, you give master a None default, and when you try to use master.config it says
'NoneType' object has no attribute 'config'
Try passing an argument in when you initialize the instance of ApplicationWindow. Whatever it is that you want master to be (just not a None object).
I have updated your code (below) and it runs. The button works, and the exit function closes the window. There was a lot to fix, but it runs without error. Take it from here:
import tkinter
class ApplicationWindow(tkinter.Tk):
def __init__(self, master=None):
# Tk.__init__(self, master)
self.master = master
self.master.geometry('800x400')
self.master.f_app = tkinter.Frame(self.master).pack()
menubar = tkinter.Menu(self.master)
self.master.config(menu=menubar)
fileMenu = tkinter.Menu(menubar)
fileMenu.add_command(label="Exit", command=self.onExit)
menubar.add_cascade(label="File", menu=fileMenu)
self.b_log = tkinter.Button(self.master, width=10, text="Войти", command=self.func).pack()
def onExit(self):
self.master.destroy()
def func(self):
print("hello")
def main():
root = tkinter.Tk()
app = ApplicationWindow(root)
root.mainloop()
if __name__ == '__main__':
main()
You have an argument named master=None defaults to None. So when you create an instance of ApplicationWindow() without parameter your master argument gets None, and here you are calling config() method but your master is none and it doesnt have a method named config.
class ApplicationWindow(Tk):
def __init__(self, master=None):
...
self.master.config(menu=menubar) # Error accurred here
def main():
# root = tk
app = ApplicationWindow() # pass an argument

OOP on Tkinter (connect classes)

I'm trying to learn OOP using tkinter. I want to create two classes, one for the main Frame and the other for a simple Label, and then grid the Label to the Frame with an OOP approach.
Something like...
import tkinter as tk
class main_frame(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self)
class label1(tk.Label):
def __init__ (self, *args, **kwargs):
tk.Label.__init__(self)
#label = tk.Label(main_frame, text='lol')???
root = tk.Tk()
main_frame(root).grid(row = 0, column = 0)
#label1(main_frame).grid(row=1,column=0) ???
root.mainloop()
My code might not be making any sense compared to a correct OOP approach to this. I appreciate any help.
The main_frame widget needs to be a child of the Tk window, and then the label1 widget needs to be a child of the of the main_frame widget. Here is my solution:
import tkinter as tk
class main_frame(tk.Frame):
def __init__(self, master, *args, **kwargs):
tk.Frame.__init__(self, master)
self.label = label1(self)
self.label.grid(row = 0, column = 0)
class label1(tk.Label):
def __init__ (self, master, *args, **kwargs):
tk.Label.__init__(self, master, text="test")
root = tk.Tk()
root.mainframe = main_frame(root)
root.mainframe.pack()
root.mainloop()
Note how each widget passes itself as master to each other widget.

Tkinter: window navigation inside a GUI

I posted a question about this topic a few days ago but since my sample code in that question was wrong I deleted that topic to come up with a cleaner sample code.
what's the best practice to navigate through different pages/windows in a GUI built by Tkinter? simply, I want to be able to go through different pages in my App, via commands from my menubar. I want to avoid stacking pages on top of each other and a method in which you use grid_remove() or pack_forget() is preferable to me.
The only other tutorial which I found here uses the stacking method and lift(). is there any other better way?
import tkinter as tk
from tkinter import *
class MainWin(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.page_1 = Page1(self.parent)
self.page_2 = Page2(self.parent)
self.init_UI()
def init_UI(self):
menubar = Menu(self.parent)
self.parent.config(menu=menubar)
self.parent.title('Frame Switching test app')
file_menu = Menu(menubar)
pages_menu = Menu(menubar)
menubar.add_cascade(label='File', menu=file_menu)
file_menu.add_command(label='Exit', command=self.on_exit)
menubar.add_cascade(label='Pages', menu=pages_menu)
pages_menu.add_command(label='Pages 1', command=self.page_1.show)
pages_menu.add_command(label='Page 2', command=self.page_2.show)
def on_exit(self):
self.quit()
class Page1(LabelFrame):
def __init__(self, parent):
LabelFrame.__init__(self, parent)
self.parent = parent
self.config(text='This is page 1 label Frame')
self.sample_text = Label(self, text='You are viewing Page 1')
def show(self):
self.pack(fill=BOTH, expand=1)
self.sample_text.grid(in_=self)
self.lift()
def close(self):
self.pack_forget()
class Page2(LabelFrame):
def __init__(self, parent):
LabelFrame.__init__(self, parent)
self.parent = parent
self.config(text='This is page 2 label Frame')
self.sample_text = Label(self, text='You are viewing Page 2')
def show(self):
self.pack(fill=BOTH, expand=1)
self.sample_text.grid(in_=self)
self.lift()
def close(self):
self.pack_forget()
def main():
root = tk.Tk()
app = MainWin(root)
root.mainloop()
if __name__ == '__main__':
main()
There's already a question and answer that shows how to stack frames. To switch to a mode where you use grid_forget or pack_forget you only have to change the code that calls lift to instead call the appropriate "forget" method on the current page (which you'll need to keep track of), and then add the new window.
If you want to create the pages on demand, and destroy them when they aren't in use, that's easy too. The only real difference is that you don't create the page until it is asked for, and delete it when you are done. Otherwise the implementation is identical.
Following is an example of creating the pages on demand. Starting with the code in this answer, modify the SampleApp class to look like this:
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# the container is where we'll pack the current page
self.container = tk.Frame(self)
self.container.pack(side="top", fill="both", expand=True)
self.current_frame = None
self.show_frame("StartPage")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
# destroy the old page, if there is one
if self.current_frame is not None:
self.current_frame.destroy()
# create the new page and pack it in the container
cls = globals()[page_name]
self.current_frame = cls(self.container, self)
self.current_frame.pack(fill="both", expand=True)

Categories

Resources