Inspired by this 300+ vote closed Q&A: Best way to structure a tkinter application?, I'm looking to avoid explicitly using root in a function within a class. I think it should be implicitly declared through self or parent or something like that. Here is the code in question:
I have this code...
self.label_this = tk.StringVar()
self.label_last = tk.StringVar()
self.label_total = tk.StringVar()
tk.Label(count_frame, textvariable=self.label_this, \
font=(None, MON_FONTSIZE)).pack(anchor=tk.W)
tk.Label(count_frame, textvariable=self.label_last, \
font=(None, MON_FONTSIZE)).pack(anchor=tk.W)
tk.Label(count_frame, textvariable=self.label_total, \
font=(None, MON_FONTSIZE)).pack(anchor=tk.W)
self.update_cnt_labels()
Then later on...
''' Get list of Window ID's on monitor now '''
new_windows = self.windows_on_monitor(new_windows)
new_windows_cnt = len(new_windows) / WIN_CNT
if self.old_windows_cnt == new_windows_cnt :
FlashMessage (self.label_this, "No new windows to remove...", \
3, 750, 250)
self.update_cnt_labels()
return
Then later on...
class FlashMessage:
def __init__(self, widget, message, count=5, on=500, off=300):
self.delay_show (1, widget, message)
for i in range(count):
self.delay_show (on, widget, "")
self.delay_show (off, widget, message)
def delay_show(self, ms, widget, message):
root.after(ms, widget.set(message))
root.update_idletasks()
I want to avoid using root in the last two lines and use self or something similar.
My program call chain is something like:
the traditional: root = tk.Tk()
bunch of mainline initialization stuff.
the class: ResizingCanvas(mycanvas)
mainline function: popup(event) which is bound to <ButtonPress-1>
Dynamically formatted menu.tk_popup(event.x_root, event.y_root)
the class: RemoveNewWindows()
the function: remove()
the class: FlashMessage() (show above)
the function: self.delay_show() (shown above)
Each class and function has haphazard self, positional parameters, *args and **kwargs which mostly serve no purpose. Indeed even the __init__ above might be unnecessary. This is a result of copying code all over stack overflow.
Every second word in the program seems to be self but the word parent is only used in the class ResizingCanvas(). Do I have to propagate parent down the call list and use it somehow?
You can call after and update_idletasks on any widget. There are many such functions that can be called on any widget but which have a global effect.
In your case, you'll need to pass some widget into the FlashMessage constructor and save the reference. You can then use the reference to call the functions.
You're passing something called widget that doesn't actually contain a widget. You need to rename it to something more appropriate (eg: var), and then pass in an actual widget.
(Note: you also are calling after incorrectly, which I've fixed in the following example)
For example:
class FlashMessage:
def __init__(self, widget, var, message, count=5, on=500, off=300):
self.widget = widget
...
def delay_show(self, ...):
self.widget.after(ms, var.set, message)
self.widget.update_idletasks()
Then, whenever you create an instance of FlashMessage you need to add a widget as the first parameter.
For example, assuming that count_frame is defined in the context where you create an instance of FlashMessage and it is an actual widget, it might look something like this:
if self.old_windows_cnt == new_windows_cnt :
FlashMessage (count_frame, self.label_this, "No new windows to remove...", \
3, 750, 250)
self.update_cnt_labels()
return
Related
I am trying to condense my code, so I want to create object instead of having to create labels each time I need one.
However, I can't figure out how to be able to change attributes of the object-labels using .config. I've tried using objectvariable.config(...), but that doesn't work. Neither does using a method like in the following:
class title_label():
def __init__(self):
self = tkinter.Label(root)
self.pack(side='left')
def update(self, text):
self.config(text=text)
Error-message is: objectvariable object has no attribute config.
How can I use .config on an object containing a label?
It should be
class title_label():
def __init__(self, root):
self.label = tkinter.Label(root) # <<< 'label' field here
self.label.pack(side='left')
def update(self, text):
self.label.config(text=text)
self hold the reference to the class itself. label is something that your class is supposed to hold not to be. Another approach would be to derive from the Label class, but for what it is worth storing the label in the field should be good enough for you.
If you made your class a subclass of tkinter.Label then it would have inherited a config() method from it.
Here's an example of how that might be done:
import tkinter as tk
class TitleLabel(tk.Label):
def update(self, text):
self.config(text=text)
if __name__ == '__main__':
root = tk.Tk()
title_lbl = TitleLabel(root, text='Initial Text')
title_lbl.pack(side='left')
root.after(1000, lambda: title_lbl.update('CHANGED!')) # Update after 1 sec.
root.mainloop()
But as you can see, there wouldn't really be much point of doing so, since the only thing update() does is forward to call on the base class.
Edit
In comments inheritance was suggested however this is already being done and I've added additional code snippet to show.
There are a few similar questions of initializing instance variables outside of __init__ where instance variables are initialized further down in the class within another def function (method). This question isn't a duplicate of those questions.
I have three classes all declaring the same self.xxxx instance variables after the def __init__:
class AskQuestion(simpledialog.Dialog):
""" Prepends "\n" to text passed.
Appends "\n\nAre you sure?\n" to text passed.
Allows text to be highlighted and copied to clipboard with CTRL+C.
Blocks other windows from getting focus
MON_FONTSIZE is temporary font size until configuration file set up.
"""
def __init__(self, parent, title=None, text=None, confirm='yes',
align='center', thread=None, icon='warning'):
self.confirm = confirm # Append "Are you sure?" line?
self.align = align # data (text lines) alignment
self.thread = thread # The thread run before button click
self.loop_no = 1 # Loop counter (not used yet)
self.data = text # data (text lines) for text box
self.text = None # Textbox widget
self.icon = icon # Warning, Error, Info, Question icons
try:
self.font = (None, MON_FONTSIZE)
except NameError:
self.font = (None, 10)
# Shared functions
self.wait_window = wait_window_func
#self.body = body(self, parent)
#self.body = body
simpledialog.Dialog.__init__(self, parent, title=title)
How can these lines of code be spun out into a global function which is called to initialize the variables? I'm searching for a technique similar to the bash . (source command) or the C #include command except variables won't be sourced from another file, simply a global function within the current file (module).
FYI I'm looking for consistency and code reduction for tkinter simpledialog class wrappers for ShowInfo, AskQuestion, AskString, etc.
in a a case where your classes share init methods you can reduce code by inherting them, like this
class A(B):
where B is a parent class, also according to the question you can inherit more than one classes,like this
class A(B,C):
I am providing a generic answer to avoid the same discussion as in comments
I'm making a GUI using the TKinter library from Python. I want the user to select an option from a Combobox and then, to press a Button, which should create an instance of a class named as the selected option. In order to save code, I decided to use the exec() fuction in this way:
exec('instance = ' + comboExample.get() + '()').
This starts the __init__() method of the class, but when I try to call an other method (in this case from an inherited class) using instance.method() it displays the following error: NameError: name 'instance' is not defined. Here you have an example of the script:
from tkinter import *
from tkinter import ttk
master = Tk()
#Create classes
class Base():
def method(self):
self.label = Label(master, text = self.sentence)
self.label.pack()
class Example1(Base):
def __init__(self):
print('Example1 created')
self.sentence = 'This is example 1.'
class Example2(Base):
def __init__(self):
print('Example2 created')
self.sentence = 'This is example 2'
#Create Combobox and Button
combo = ttk.Combobox(master, state = 'readonly')
combo['values'] = ['Example1', 'Example2']
combo.pack()
def callback():
exec('instance = ' + combo.get() + '()')
#Here is the error
instance.method()
button = Button(master, command = callback, text = 'Button')
button.pack()
master.mainloop()
I don't now why but when I try with the following code it works properly:
class Example():
def __init__(self):
self.text = 'This is an example'
def add_text(self):
print(self.text)
exec('instance = Example()')
instance.add_text()
At the moment, I've only found one solution, which consists in not using exec(), but makes me waste more code than using it, especially if I want to create a lot of classes like Example1 and Example2. It's all like the previous big script, but changing the callback() function:
def callback():
if combo.get() == 'Example1':
instance = Example1()
if combo.get() == 'Example2':
instance = Example2()
instance.method()
That's all. I started programming in Python only 2 months ago and I'm also new in stackoverflow, so if I've made some mistake in the explanation or anything, please tell me and I'll fix it.
Thanks for your time. Any help would be appreciated.
The issue isn’t your syntax; it’s that you’re trying to do something illegal. You can’t create new local variables with exec. (The reason the same code outside a function works is that in general you can create a new global variable with exec, but it’s still a bad idea.)
But you also don’t need to do that. In Python, everything is an object, including classes. So, you just need the get the class from the name. Then you can create an instance of that class, and store it in a local variable, by just using the same normal syntax you’d use for instantiating a class statically and storing it in a local variable.
The right way to do this is to store a dictionary mapping names to class objects. If you want to get clever, you can write a decorator that registers classes with that dictionary, but if that sounds like Greek to you, just do it explicitly:
classes = {'Spam': Spam, 'Eggs': Eggs}
If you have dozens of these, you can avoid the repetition with a comprehension like this:
from your_module import Spam, Eggs
classes = {cls.__name__: cls for cls in (Spam, Eggs)}
… but at that point you’re probably better off learning how to write the decorator.
Either way, you can fill your combo box with the keys of that dictionary instead of repeating yourself in the combo['values'] line.
And then, to create an instance, you just do this:
cls = classes[comboExample.get()]
instance = cls()
(Obviously you can collapse that into a single line, but I thought it would be easier to understand if we keep the two parts separate.)
If you really want to do this in a hacky way, you can. Every class that you’ve created in this module is already stored in a dictionary by name—the module’s global namespace. That’s the same place you were trying to find it implicitly with exec, but you can find it explicitly by just looking it up in globals(). However, the global namespace also has the names of all of your functions, imported modules, top-level constants and variables, etc., so this is usually a bad idea. (Obviously, exec has the exact same problems.)
You should not be using exec for this purpose. exec is a powerful tool, but it's the wrong tool for this job.
A much simpler approach is to create a mapping from user inputs to classes. You can then use that mapping both for the combobox and for the callback.
Example:
...
mapping = {"Example1": Example1, "Example2": Example2}
#Create Combobox and Button
combo = ttk.Combobox(master, state = 'readonly')
combo['values'] = sorted(mapping.keys())
combo.pack()
def callback():
class_name = combo.get()
cls = mapping[class_name]
instance = cls()
instance.method()
...
You could even automatically generate the mapping by iterating over a list of classes, though for this example that seems like overkill.
To make available an instance of the ttk.Style() class, it was illustrated in this tkinter guide that the syntax is:
import ttk
s=ttk.Style()
When typing these command in IDLE, I noticed that ttk.Style() actually has a predefined argument, i.e.
s=ttk.Style(master=None)
I have written the following test script:
import tkinter as tk
import tkinter.ttk as ttk
class App(ttk.Frame):
def __init__(self, parent):
ttk.Frame.__init__(self, parent, style='App.TFrame', relief=tk.SUNKEN,
border=10)
self.parent = parent
self.__createStyle()
self.__createWidgets()
def __createStyle(self):
self.s = ttk.Style()
self.s.configure('.', background='orange', border=100)
self.s.configure('App.TFrame', background='yellow')
self.s.configure('Btn.TButton', background='light blue', border=10)
def __createWidgets(self):
self._label = ttk.Label(self.parent, text='Label packed in root.')
self._label.pack()
self._btn = ttk.Button(self, style='Btn.TButton', command=self.__click,
text='Button packed inside self or class App, which is a ttk.Frame')
self._btn.pack()
def __click(self):
return print('Left Button Clicked!')
class myWidget(ttk.Frame):
def __init__(self, parent):
ttk.Frame.__init__(self, parent, style='my.TFrame', relief=tk.GROOVE,
border=10)
self.parent = parent
self.__createStyle()
self.__createWidgets()
def __createStyle(self):
self.s = ttk.Style()
self.s.configure('my.TFrame', background='purple')
self.s.configure('my.TLabel', background='pink', border=10)
self.s.configure('my.TEntry', foreground='red', border=10)
def __createWidgets(self):
self._label = ttk.Label(self, style='my.TLabel',
text='myWidget Label packed in self or class myWidget, which is a ttk.Frame.')
self._label.pack()
self._entry = ttk.Entry(self, style='my.TEntry')
self._entry.pack()
if __name__ == "__main__":
root = tk.Tk()
root.title('Test Style')
root.geometry('500x150')
a = App(root)
a.pack(fill='both', expand=1)
b = myWidget(a)
b.pack()
root.mainloop()
Question 1: When do I need to declare the master arguement in ttk.Style()? E.g. in the above script, if I write self.s = ttk.Style() and self.s = ttk.Style(master=self.parent) in class myWidget, I get the same result (see Fig1).
Question 2: Is there a need to prefix s=ttk.Style() with self? I get the same result as shown in Fig1 with and without the self prefix.
Question 3: If I rename 'my.TFrame' in class myWidget as 'App.TFrame'(this name was used in class App), the background colour of the class App changed to purple color too (same color as class myWidget. Why did this happened given that variable name in different classes are unique?
Question 4: The names 'App.TFrame' and 'my.TFrame' were called before it was declared. Why did python or tkinter not complain or give an error but allowed the script to execute?
Figure 1
Figure 2
When do I need to declare the master arguement in ttk.Style()?
Probably never, except the case when tkinter doesnt support the default root. When you pass None as the master, the master becomes the current root instance of Tk class.
The main purpose of the master (root or any tk-widget) is to delegate instance of tk to the Style, so that the Style could be able to execute Tcl-related commands.
No more, no less.
Is there a need to prefix s=ttk.Style() with self?
It depends on your requirements. In context of your code - self is meaningless, because you're setting up styles in a scope of the __createStyle function.
Otherwise, if you wish to keep the reference, it makes sense to prefix with self.
If I rename my.TFrame in class myWidget as App.TFrame(this name was used in class App), the background colour of the class App changed to purple color too (same color as class myWidget. Why did this happened given that variable name in different classes are unique?
Because both of classes share the same frame style, hence the same color. Created style is a global thing, it can be chaged at runtime, and all the relevant widgets will react to these chages.
The names App.TFrame and my.TFrame were called before it was declared. Why did python or tkinter not complain or give an error but allowed the script to execute?
Why you think they should? When you pass something like <any_sensible_name>.<any_relevant_and_existing_basestyle>, ttk knows that you want a variation of a base style, so it's implicitly creates one, which inherits all base properties.
Try that trick with something more meaningless, like your current style name without dot (ttk.Frame.__init__(..., style='AppTFrame', ...)), which gives you the desired error:
_tkinter.TclError: Layout AppTFrame not found
Only a partial Answer, but I suppose #Bryan Oakley will entlighten us sooner or later.
Question 3:
If you use "App.TFrame" instead of "my.TFrame" inside your MyWidget Class, you override the predefined style properties.
Short example:
If you style "TFrame", all "TFrame"( == Tkinter.Frame/ttk.Frame ) instances will be affected.This is also sometimes referred to as "root-Style".
If you define another "somename.TFrame" and set it for one Object of type frame, it will be styles according "somename.TFrame".
Question 4:
The lookup names only override default styles. As long as they have no properties, they do not override a thing.
This "assignment" results in a tcl call and has no specific error handling inside the Tkinter / ttk Sources (used in BaseWidget class).
I can only tell that tcl does not throw an error here but I am not a tcl expert myself.
I hope this at least helps a bit.
As the title says, I'm writing a Console Menu Generator in Python. I have 2 classes, Menu and Item. But I get into troubles. Here is the code:
class Menu:
def AddItem(self,item):
class Item:
def __init__(self,text,ToDoNext):
self.text=text
??????????????
self.item.append(Item())
def Show():
for i in range(len(self.item)):
print(str(i+1)+") "+str(self.item[i])+"\n")
print("0) Back\n")
option=int(input())
self.item[option].????????????
This code basically do the next:
Main=Menu()
Menu.AddItem("Open file",ToDo1)
Menu.AddItem("Save file",ToDo2)
Menu.Show()
'''1) Open file
2) Save file
0) Back
_
'''
If I write 1 and press enter should do the portion of code ToDo1, for example.
The solution that I thought is the nextone:
def ToDo1():
print("Hello, world!")
Menu.AddItem("Say Hello","ToDo1()")
and use an eval() function inside the Show().
But I'm not pretty sure this is not the correct way to do that.
I would like you to show me a better way, and if you have ever do something like that (Console Menu Generator) to share the code and see another way of doing the same.
I absolutely recommend creating a class Item, even if you only have text and function attributes!
Who knows what kind of complex logic you will need later on.
With this in mind, creating a menu would probably look something like this:
main = Menu()
main.AddItem(Item("Open", openFile))
main.AddItem(Item("Close", closeFile))
Also, on top of your text and function attributes, you should add parent attribute to the Item class. parent simply points at the parent menu of our item:
main = Menu()
# automatically calls main.AddItem(item1)
open = Item("Open", openFile, main)
# automatically sets parent to main
main.Add(Item("Close", closeFile))
Now that we know how a proper Menu and Item should work, we can start coding the classes.
Menu
This shouldn't be too hard, all we need are add_item(), remove_item() and draw() methods and a list of items.
Also it would be good to draw our menu's name, so lets add name attribute.
class Menu:
def __init__(self, name, items=None):
self.name = name
self.items = items or []
def add_item(self, item):
self.items.append(item)
if item.parent != self:
item.parent = self
def remove_item(self, item):
self.items.remove(item)
if item.parent == self:
item.parent = None
def draw(self):
print(self.label)
for item in self.items:
item.draw()
Obviously we could code much more methods and attributes for our menu, but that includes all the essential methods.
Item
Item class should be even easier, it hardly needs any methods at all.
Item obviously needs a name and a function (function will be ran when item gets activated), on top of that it has the earlier mentioned parent attribute.
We probably should create a setter for parent, which would automatically move the item under an other menu, but I'll leave that for you if you want to do it.
Also don't forget the draw()-method for item too, we must be able to draw our items the way they want to be drawn, not the way our Menu wants to draw them.
class Item:
def __init__(self, name, function, parent=None):
self.name = name
self.function = function
self.parent = parent
if parent:
parent.add_item(self) # use add_item instead of append, since who
# knows what kind of complex code you'll have
# in add_item() later on.
def draw(self):
# might be more complex later, better use a method.
print(" " + self.name)
Final thoughts
We've now finished our menu, it works and you should be able to use it as a basic menu.
However, the superior console menu would only have one class called MenuItem.
Each item's parent would be an other MenuItem (each, but the root MenuItem's of course) and the menu would look something like this when it's drawn:
[-] Root
[+] Submenu of Root
[-] An other submenu of Root
This menu runs functions, others open/close
<This menu has focus on it>
Select this menu by pressing mousedown
[+] A third submenu of Root
Where not giving function parameter would create items list and allow users to close/open the menuitem.
If function is given, it will work normally and only execute the function when selected.
To go even a step further, we would have separated MenuItem and two subclasses: ActionMenu and ContainerMenu.
But please keep in mind, this is somewhat hard to code and not for beginners. You might wanna stick with the first version I went through.
Functions can be passed around freely in Python. If you say AddItem("Say Hello", ToDo1), you pass the value ToDo1, which is a function object. You can then store it in self.function, later fish it with fn = self.item[option].function, and then call it with fn(). It's all clearer when you realize that a regular function call like do_stuff() is actually two things: first getting the function object from the variable do_stuff (which is typically a never-modified global variable), then calling this function object.
Here is a working example
usually the Class Menu part would be in another file called "myMenu" and imported with the command from myMenu import myMenu
items is an array of dictionaries. Each list item has a dictionary with two entries, "text" and "func"
The input is called as n-1 as arrays start at zero
import sys
class myMenu:
items=[]
def AddItem(self,text,function):
self.items.append({'text': text, 'func':function})
def Show(self):
c=1
for l in self.items:
print c, l['text'],"\n"
c = c +1
def Do(self,n):
self.items[n]['func']()
def clist():
print "cheeses are wensleydale and cheddar\n"
def bye():
print "bye"
sys.exit(0)
if __name__ == "__main__":
m=myMenu()
m.AddItem("cheese",clist)
m.AddItem("quit",bye)
while(True):
m.Show()
n=input("choice>")
m.Do(n-1)
If you want to have the menu items as a class instead of a dictionary, then declare the class immediately after class MyMenu, so something like this (not tested)
class myMenu:
items=[]
class Item:
func=None
text="default"
def __init__(self,t,f):
self.text=t
self.func=f
def AddItem(self,text,function):
self.items.append(Item(text,function))
for future readers! it's easy to have a python console menu using console-menu.
pip install console-menu
now, let's implement our menu
from consolemenu import *
from consolemenu.items import *
# Create the menu
menu = ConsoleMenu("Title", "Subtitle")
# Create some items
# MenuItem is the base class for all items, it doesn't do anything when selected
menu_item = MenuItem("Menu Item")
# A FunctionItem runs a Python function when selected
function_item = FunctionItem(
"Call a Python function", input, ["Enter an input"]
)
# A CommandItem runs a console command
command_item = CommandItem("Run a console command", "touch hello.txt")
# A SelectionMenu constructs a menu from a list of strings
selection_menu = SelectionMenu(["item1", "item2", "item3"])
# A SubmenuItem lets you add a menu (the selection_menu above, for example)
# as a submenu of another menu
submenu_item = SubmenuItem("Submenu item", selection_menu, menu)
# Once we're done creating them, we just add the items to the menu
menu.append_item(menu_item)
menu.append_item(function_item)
menu.append_item(command_item)
menu.append_item(submenu_item)
menu.show()
you would get a menu like this