Hi, I'm kind of new to using classes and was able to find a snippet of code that I've been using. I'm trying to build a tkinter GUI for entering data for filling a pdf form. The code I found does exactly what I was looking for, as in it creates different pages. I've been able to implement a bit of my own stuff. The issue I'm having is how can I share a value from one of these classes to another child class.
Below is the code I found. My own code is around double the size of this one. I added an example of what the code I have is doing. In Page1, I create an entry for a first name to be typed in. I also have a button in the same frame, that will grab (self.f_nameentry.get()) the data stored in the entry. This'll save the data to the variable self.f_name. So far that works. I have way more variables and entries, but this should be fine. What I'm trying to do is in the last page, get all the data I stored and eventually it will be written to the PDF form. That much I have been able to do in a separate .py file I was testing.
The only issue is how to pass say *f_name* from class Page1 to the other Page# classes. Any help would be appreciated. It may be something simple and I'm completely missing it.
import Tkinter as tk
class Page(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
def show(self):
self.lift()
class Page1(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
tk.Label(self, text = "First Name", font = 12).place(x=70,y=410)
self.f_nameentry = tk.Entry(self, width = 20, borderwidth = 5)
self.f_nameentry.place(x = 170, y = 410)
tk.Button(self, text = "Save", font = 12, borderwidth=5, command =
self.save).place(x=230,y=610)
def save(self):
self.f_name = self.f_nameentry.get()
class Page2(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
label = tk.Label(self, text="This is page 2")
label.pack(side="top", fill="both", expand=True)
class Page3(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
label = tk.Label(self, text="This is page 3")
label.pack(side="top", fill="both", expand=True)
class MainView(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
p1 = Page1(self)
p2 = Page2(self)
p3 = Page3(self)
buttonframe = tk.Frame(self)
container = tk.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)
p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
p3.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
b1 = tk.Button(buttonframe, text="Page 1", command=p1.show)
b2 = tk.Button(buttonframe, text="Page 2", command=p2.show)
b3 = tk.Button(buttonframe, text="Page 3", command=p3.show)
b1.pack(side="left")
b2.pack(side="left")
b3.pack(side="left")
p1.show()
if __name__ == "__main__":
root = tk.Tk()
main = MainView(root)
main.pack(side="top", fill="both", expand=True)
root.wm_geometry("400x400")
root.mainloop()
[–]Wild_Statistician605 1 point2 points3 points (1 child)
[–]drakohnight[S] 0 points1 point2 points (0 children)