I have this code to change the title of the frame. I have two pages here. In PageOne
class, we get the title and by pressing the button, the title changes. And, we can use the Go to page two
button to navigate to PageTwo
. here also we can do the same.
Here is my question, how I can define only one function instead of using function add1()
in PageOne
, and function add2()
in PageTwo
. I mean I only define one function on PageOne
and use the same function in PageTwo
, and show the results in the frame2
.
import tkinter as tk
class Data:
def __init__(self):
self.number1 = tk.StringVar()
self.number2 = tk.StringVar()
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.minsize(700, 700)
container = tk.Frame(self)
container.pack()
self.data = Data()
self.frames = {}
for F in (PageOne, PageTwo):
frame = F(container, self.data)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.frames[PageOne].page_button.config(command=self.go_to_page_two)
self.show_frame(PageOne)
def go_to_page_two(self):
self.show_frame(PageTwo)
def show_frame(self, c):
frame = self.frames[c]
frame.tkraise()
class PageOne(tk.Frame):
def __init__(self, parent, data):
super().__init__(parent)
self.data = data
entry1 = tk.Entry(self, textvariable=self.data.number1)
entry1.pack()
self.button1 = tk.Button(self, text="click", command=self.add1)
self.button1.pack()
self.frame1 = tk.LabelFrame(self, height=200, width=200, borderwidth=2)
self.frame1.pack()
self.page_button = tk.Button(self, text="Go to Page Two")
self.page_button.pack()
def add1(self):
self.frame1.config(text=str(self.data.number1.get()))
class PageTwo(tk.Frame):
def __init__(self, parent, data):
super().__init__(parent)
self.data = data
entry2 = tk.Entry(self, textvariable=self.data.number2)
entry2.pack()
self.button2 = tk.Button(self, text="click", command=self.add2)
self.button2.pack()
self.frame2 = tk.LabelFrame(self, height=200, width=200, borderwidth=2)
self.frame2.pack()
def add2(self):
self.frame2.config(text=str(self.data.number2.get()))
app = SampleApp()
app.mainloop()
CodePudding user response:
you can do in this way:
import tkinter as tk
class Data:
def __init__(self):
self.number = tk.StringVar()
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.minsize(700, 700)
container = tk.Frame(self)
container.pack()
self.data = Data()
self.frames = {}
for F in (PageOne,):
frame = F(container, self.data)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
def show_frame(self, c):
frame = self.frames[c]
frame.tkraise()
class PageOne(tk.Frame):
def __init__(self, parent, data):
super().__init__(parent)
self.data = data
entry1 = tk.Entry(self, textvariable=self.data.number)
entry1.pack()
self.button1 = tk.Button(self, text="click", command=lambda : self.add(self.frame1, data.number))
self.button1.pack()
self.frame1 = tk.LabelFrame(self, height=200, width=200, borderwidth=2)
self.frame1.pack()
self.button2 = tk.Button(self, text="click", command=lambda : self.add(self.frame2, data.number))
self.button2.pack()
self.frame2 = tk.LabelFrame(self, height=200, width=200, borderwidth=2)
self.frame2.pack()
def add(self, frame, data):
frame.config(text=str(self.data.number.get()))
app = SampleApp()
app.mainloop()