Passing class values to another class using Tkinter in Python 3.5 - python

I have the following code (example of my real program):
from tkinter import *
def class1(Frame)
def nv(self,x):
self.vent=Toplevel(self.master)
self.app=class2(self.vent)
self.value=x
def __init__(self,master):
super().__init__(master)
self.master=master
self.frame=Frame(self.master)
self.btn=Button(self, text="example", command=lambda: self.nw(1))
self.btn.pack()
self.pack()
def class2(Frame):
def __init__(self, master):
super().__init__(master)
self.master=master
self.frame=Frame(self.master)
self.value=class1.nw.value.get()
root= Tk()
marco=Frame(root)
marco.pack
lf=class1(marco)
root.mainloop()
this last part is the problem, i cant use .get() properly for this problem, I want to get the value of x when I create the new window.
I use lambda so i can execute the command with parameters.
So the question is, is there a way for me to access the value of x in class 2?

You appear to be quite confused about the usage of classes when using tkinter. super() should not be used with tkinter as explained here and when declaring a class you should use the class keyword, not def. .get() is a method of a tkinter variable class such as tkinter.IntVar, tkinter.StringVar, etc. so is not needed in the example you have given.
What you need is the function in the Frame you are trying to get the value of x from (nv) and then parse that value to the __init__ method in the child Frame.
Here is my solution:
from tkinter import *
class Class1(Frame):
def nv(self,x):
self.vent = Toplevel(self.master)
self.app = Class2(self.vent,x)
def __init__(self,master):
Frame.__init__(self,master)
self.master = master
self.btn = Button(self, text="example", command=lambda: self.nv(1))
self.btn.pack()
self.pack()
class Class2(Frame):
def __init__(self, master, x):
Frame.__init__(self,master)
self.master=master
self.frame=Frame(self.master)
self.x_text = Label(self, text=str(x))
self.x_text.pack()
self.pack()
root = Tk()
marco = Frame(root)
marco.pack()
lf = Class1(marco)
root.mainloop()

Related

Object oriented Tkinter

I am trying to write a gui which as a class which is the main application. A single instance of that class is created in the main root. At the same time I want a submit button to be clicked where some values are verified before a further submission to write the data. I am trying to do this by creating a new class for the Toplevel pop up window. But I am not sure how best to structure this. Ideally an instance of the pop up window class would be created each time the button is selected. It seems like with the way I have structured it another instance of the main application class has been created. I am a little confused how to correctly do this using OOP.
Below is some sample code to illustrate the problem.
import tkinter as tk
from tkinter import ttk
class Window(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.title = "TITLE"
self.master = master
self.submit = ttk.Button(self, text = 'SUBMIT', command = self.click_submit_button)
self.submit.grid(row = 0, column = 2, padx = 20, pady = 20)
def click_submit_button(self):
self.submit_pop_up = submit_button(self.master)
print('New PopUp')
class submit_button(tk.Toplevel):
def __init__(self, master):
tk.Toplevel.__init__(self, master)
self.master = master
self.title = 'TITLE'
if __name__ == "__main__":
root = tk.Tk()
app = Window(root)
app.pack()
root.mainloop()
There is something missing from my understanding of the best approach to using OOP to structure a program like this.
Solution
Just make the submit_button class inherit the Window class and instantiate only the submiit_button class. With this, you don't need to instantiate the Window class. This allows for a special trick so that you can access the attributes of the submit_class class in the Window class without creating an instance. Just use self since it is actually an instance of the submit_class passed on to the Window class. Here is your code with that. There are many other suggestions and to know, see the code.
Suggestions
In the first place, why are you making submit_button a separate class? You could include it as a method of Window class. If you have good reason, it is ok but otherwise make it a method.
And also, why are you creating a root window and making the window class save it as an attribute? Just make the Window class inherit tk.Tk instead of tk.Frame. You can then create frame inside the __init__ function. Here is the code working code with these rectifications:
Code
import tkinter as tk
from tkinter import ttk
class Window(tk.Tk):
def __init__(self):
super().__init__()
self.title("TITLE")
self.submit = ttk.Button(self, text = 'SUBMIT', command = self.click_submit_button)
self.submit.grid(row = 0, column = 2, padx = 20, pady = 20)
class submit_button(Window):
def __init__(self):
super().__init__()
self.submit_pop_up = tk.Toplevel(self)
self.submit_pop_up.withdraw()
print(self.submit_pop_up)
def click_submit_button(self):
self.submit_pop_up.deiconify()
print('New PopUp')
if __name__ == "__main__":
app = submit_button()
app.mainloop()
Is that what you want? You should used self.master for all widgets.
import tkinter as tk
from tkinter import ttk
class Window(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.master = master
self.master.title( "TITLE")
self.submit = ttk.Button(self.master, text='SUBMIT', command=self.click_submit_button)
self.submit.grid(row=0, column=2, padx=20, pady=20)
def click_submit_button(self):
self.submit_pop_up = submit_button(self.master)
print('New PopUp')
class submit_button(tk.Toplevel):
def __init__(self, master):
tk.Toplevel.__init__(self, master)
self.master = master
self.master.title('TITLE')
if __name__ == "__main__":
root = tk.Tk()
app = Window(root)
#app.pack()
root.mainloop()
Result:

I've created a "clear" button in my GUI I'm working on using tkinter. How do I get this button to clear text?

The problem I'm running into is trying to clear the text entered, when the clear button is pressed. I understand there's something called command binding, and passing in a function that could clear the text as a reference to the command. I'm just a bit confused as to go about doing so.
Here's the relevant code:
class Window(Frame):
def __init__(self, master):
super().__init__(master)
master.title('Learning Python')
master.configure(background='black')
master.geometry('900x200')
master.resizable(0, 0)
class Submit(Button):
def __init__(self, master):
super().__init__(master)
self.configure(text='Submit', background='black', foreground='light green', highlightthickness=0,
border=0)
class Clear(Button):
def __init__(self, master):
super().__init__(master)
self.configure(text='Clear', background='black', foreground='light green', highlightthickness=0,
border=0)
class ProgressBar(ttk.Progressbar):
def __init__(self, master):
super().__init__(master)
self.config(orient='horizontal', maximum=1462, mode='determinate')
class PagesRead(Label):
def __init__(self, master):
super().__init__(master)
self.config(text='How many page(s) did you read?', background='black', foreground='light green')
class EntryBox(Entry):
def __init__(self, master):
super().__init__(master)
if __name__ == '__main__':
root = tk.Tk()
app = Window(root)
bar = ProgressBar(root)
bar.pack(fill=tk.BOTH)
pages = PagesRead(root)
pages.pack()
entry = EntryBox(root)
entry.pack()
submit = Submit(root)
submit.pack()
clear = Clear(root)
clear.pack()
app.mainloop()
class Clear(Button):
def __init__(self, master):
super().__init__(master)
self.configure(
command=clear_callback
text='Clear',
background='black',
foreground='light green',
highlightthickness=0,
border=0
)
Then you could do something like this:
def clear_callback(self, event=None):
self.entry.delete(0, "end")
You can also bind events, there's some great information on this page
Explicit bindings pass an additional argument in the form of a tkinter event object, so sometimes you might need to use a kwarg, or a lambda expression to pass the extra argument
app.bind(
"<Return>",
lambda: clear_callback(event)
)
Hope this was helpful. Cheers.
Even if you have created a new class for the Entry, it still retains the Entry methods. So you can simply call the delete method:
clear = Clear(root)
clear.config(command=lambda:entry.delete(0, 'end'))
clear.pack()
Depending on your taste you could instead create a clear() method for the EntryBox class and call it without parameters:
class EntryBox(Entry):
def __init__(self, master):
super().__init__(master)
def clear(self):
self.delete(0, 'end')
and then command the Button to this method:
clear = Clear(root)
clear.config(command=entry.clear)
clear.pack()

How to get Tkinter Python3.x to nest grids?

I've been trying for days to figure out how to just put grids in grids of objects. I've got two frames (which I guess are widgets in Tk?) I add one to the other, but the position of its widgets don't appear to respect the parent widgets (and it just overwrites them).
here is my MCVE
import tkinter as tk
class TestDoubleNested(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.grid()
self.test_label = tk.Label(text="AAAAAAAAA")
self.test_label.grid(row=0, column=0)
class TestNested(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.grid()
self.test_label = tk.Label(text="top_level_test_label")
self.test_label.grid(row=0, column=0)
self.test_label2 = tk.Label(text="top_level_test_label2")
self.test_label2.grid(row=0, column=1)
# expected to be in 3rd column...
self.test = TestDoubleNested(master)
self.test.grid(row=0, column=4)
test = TestNested()
test.master.title("Test Example")
test.master.maxsize(1000, 400)
test.master.wm_geometry("400x300")
test.mainloop()
No matter how I move around self.grid() invokations or change the column stuff, the display is the same:
As you can see AAAAAAAA displays on top of another widget from the parent, where ideally it should display to the side of everything.
You aren't specifying the master for each widget, so they are all being added to the root window. If you want widgets to be inside frames, you must specify the frame as the first argument when creating the widgets:
class TestDoubleNested(tk.Frame):
def __init__(self, master=None):
...
self.test_label = tk.Label(self, text="AAAAAAAAA")
...
class TestNested(tk.Frame):
def __init__(self, master=None):
...
self.test_label = tk.Label(self, ...)
self.test_label2 = tk.Label(self, ...)
self.test = TestDoubleNested(self)
...

return self.func(*args) TypeError: menu() missing 1 required positional argument: 'self'

I keep getting this error and I cant seem to fix it, if anybody could help me I would really appreciate it. I have been looking at it for quite a while now and I can't seem to get my head around it, I am still quite new to programming in an Object Oriented Way.
Thanks & Merry Christmas
Welcome Page
from External_Menu import *
from tkinter import *
class Welcome(Frame):
def __init__(self, root):
Frame.__init__(self, root)
self.welcome_button()
self.pack()
def welcome_button(self):
self.welcome = Button(self, text="Welcome!", command=ExternalMenu.menu)
self.welcome.pack()
self.pack()
if __name__ == "__main__":
root = Tk()
main = Welcome(root)
main.mainloop()
External Menu
from tkinter import *
class ExternalMenu(Frame):
def __init__(self, root):
Frame.__init__(self, root)
self.menu()
self.pack()
def menu(self):
self.external_menu_lbl = Label(self, text="External Menu", font=("", 26))
self.external_menu_lbl.pack()
self.sign_in_button = Button(self, text="Sign In")
self.sign_in_button.pack()
self.sign_up_button = Button(self, text="Sign Up")
self.sign_up_button.pack()
self.pack()
Your menu is a method so it needs an object it can manipulate in order to work. But you're trying to call it without an object created for it, essentially you're calling it like a function. First you need to create an object of the class that method is defined on:
an_ex_men = ExternalMenu(root)
and then you are able to call menu method on an_ex_men:
an_ex_men.menu()
But since you already call menu under your ExternalMenu's __int__ method it is called as soon as an object instance for that class is created. Shortly, you creating an ExternalMenu object is enough to achieve that as in, even without putting it to a variable to refer later:
ExternalMenu(root)
Since you want to swap between two windows you need an additional method to either hide or completely destroy the other window. Let's say you want to destroy the other window, and for that you could use a method defined for your button that does those 2 actions:
def welcome(self):
self.destroy()
ExternalMenu(root)
As in:
...
def welcome_button(self):
self.welcome = Button(self, text="Welcome!", command=self.welcome)
self.welcome.pack()
def welcome(self):
self.destroy()
ExternalMenu(root)
...
Below example does exactly what you expect, first I created a parent class App as according to you the two frames you have are in the same level of hierarchy and having a parent for them is in my opinion better structured:
import tkinter as tk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.welcome_frame = Welcome(self)
#I believe it's better to call geometry managers as parent
self.welcome_frame.pack()
#assigning parent method as button command as it affects siblings
self.welcome_frame.button['command'] = self.go_ex_men
def go_ex_men(self):
self.welcome_frame.destroy()
self.ex_men = ExternalMenu(self)
self.ex_men.pack()
class Welcome(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.button = tk.Button(self, text="Welcome!")
self.button.pack()
class ExternalMenu(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.external_menu_lbl = tk.Label(self, text="External Menu", font=("", 26))
self.external_menu_lbl.pack()
self.sign_in_button = tk.Button(self, text="Sign In")
self.sign_in_button.pack()
self.sign_up_button = tk.Button(self, text="Sign Up")
self.sign_up_button.pack()
if __name__ == "__main__":
root = App()
root.mainloop()

How to call a function whenever global variable changes it value?

How can I call a function change_label whenever global variable a changes its value? With change_variable I am trying to simulate actual changing of the variable (the variable changes on button click).
from tkinter import *
a = 3
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.master = master
self.button = Button(self.master, text='Change Variable', command=self.change_variable)
self.button.grid(row=0)
self.label = Label(self.master, text='Test')
self.label.grid(row=1)
def change_label(self):
self.label.config(bg='Red', fg='Yellow')
def change_variable(self):
global a
a = 1
def main():
root = Tk()
Application(root)
root.mainloop()
if __name__ == '__main__':
main()
If you use one of tkinters special variables (StringVar, etc) you can add a "trace" that will trigger a callback whenever the variable is set or unset.
For example:
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
...
self.a = tk.IntVar(value=3)
self.a.trace("w", self.change_label)
...
def change_label(self, *args):
self.label.config(bg='Red', fg='Yellow')
def change_variable(self):
self.a.set(1)
With that, whenever you set the value of self.a via the set method, the function bound with the trace will be called.
Any widget that uses that variable also will be updated. For example, change your label to this:
self.label = tk.Label(self.master, textvariable=self.a)
When you click the button, notice that the label changes to reflect the change.
For a description of what arguments are passed to the trace function, see What are the arguments to Tkinter variable trace method callbacks?
These variables have a good description here: The Variable Classes (BooleanVar, DoubleVar, IntVar, StringVar)
If you are using Tk, this might be worth looking into: Tk's "Variable" classes
If that doesn't work for you (because you want to store your own type, or something like that), Shiva's comment is the way to go, and if you want to store multiple variables, this might be a good idea:
class Storage(dict):
def __getattribute__(self, name):
return self[name]
def __setattr__(self, name, value):
print(name, value) # react to the change of name
self[name] = value
storage = Storage({a: 3, b: 2})
storage.a = 4
print(storage.a)
If you don't want to be able to set the variable without triggering some code you put there, good luck. You can override __setitem__ too, but you can always call dict.__setitem__ with the Storage variable as the first argument.
Try running this code it will give you the idea of what you want.
import tkinter
count = 5
def change_text():
global count
if count != 2:
button.config(text='edit')
frame = tkinter.Frame(height=500,width=500)
button = tkinter.Button(frame,text='save',command=change_text)
button.place(x=0,y=0)
frame.pack()
frame.mainloop()
The code was supposed be like this.
from tkinter import *
a = 3
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.master = master
self.button = Button(self.master, text='Change Variable', command=self.change_label)
self.button.grid(row=0)
self.label = Label(self.master, text='Test')
self.label.grid(row=1)
def change_label(self):
global a
a = 1
if a != 3:
self.label.config(bg='Red', fg='Yellow')
def main():
root = Tk()
Application(root)
root.mainloop()
hope that is what you wanted.

Categories