How To Pass Tkinter Entry Value From One Frame To Another Through My Switch Frame Function
My Tkinter application have added Notebook and inside the notebook I want to switch the frame using a button. Implemented notebook switch and frame switch. i want to take entry inp
Solution 1:
Your button uses method from master self.master.switch_frame() which only switch tabs
self.button = Button(self, text="OK", command=lambda: master.switch_frame(Tab1_Frame2))
but it could use own method which will use self.master.switch_frame() to switch tabs and it will also move value(s) from current tab directly to new tab:
self.button = tk.Button(self, text="OK", command=self.switch_frame)
defswitch_frame(self):
# get value from Entry in current Tab
value = self.uidentry.get()
# switch tab
self.master.switch_frame(Tab1_Frame2)
# use value directly in new Tab
self.master._frame.uidentry = value
self.master._frame.label['text'] = value
or to master and other tabs can get it in __init__ :
self.button = tk.Button(self, text="OK", command=self.switch_frame)
defswitch_frame(self):
# keep uidentry in master
self.master.uidentry = self.uidentry.get()
# switch tab
self.master.switch_frame(Tab1_Frame2)
classTab1_Frame2(tk.Frame):
def__init__(self, *args, **kwargs):
# get uidentry from master
self.uidentry = self.master.uidentry
self.label['text'] = self.master.uidentry
Every Tab may have different code in switch_frame
It is good to create this variable in master at start with default value.
Here master is Tab1 but using master.master you can keep it in NoteBook, and using master.master.master in RootApp
Full code:
import tkinter as tk
from tkinter import ttk
classRootApp(tk.Tk):
def__init__(self):
super().__init__()
self.geometry("640x480")
self.title("My Host APP")
self._frame = None
self.switch_frame(NoteBook)
defswitch_frame(self, frame_class):
new_frame = frame_class(self)
if self._frame:
self._frame.destroy()
self._frame = new_frame
self._frame.pack()
classNoteBook(tk.Frame):
def__init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.notebook = ttk.Notebook()
self.tab1 = Tab1(self.notebook)
self.notebook.add(self.tab1, text="User Bash History")
self.notebook.pack()
defswitch_tab1(self, frame_class):
new_frame = frame_class(self.notebook)
self.tab1.destroy()
self.tab1 = new_frame
classTab1(tk.Frame):
def__init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.uid = ''# default value at start
self._frame = None
self.switch_frame(Tab1_Frame1)
defswitch_frame(self, frame_class):
new_frame = frame_class(self)
if self._frame:
self._frame.destroy()
self._frame = new_frame
self._frame.pack()
# first frame for Tab1classTab1_Frame1(tk.Frame):
def__init__(self, *args, **kwargs):
global uid
super().__init__(*args, **kwargs)
self.label = tk.Label(self, text="Userbash history")
self.label.pack()
self.userid = tk.Label(self, text ="User ID", bg="blue", fg="white")
self.userid.pack()
self.newWindow = None
self.uidentry = tk.Entry(self, bd=5)
self.uidentry.pack()
uid = self.uidentry.get()
# button object with command to replace the frame
self.button = tk.Button(self, text="OK", command=self.switch_frame)
self.button.pack()
defswitch_frame(self):
# get value from Entry in current Tab
value = self.uidentry.get()
# keep value in master
self.master.uid = value
# switch tab
self.master.switch_frame(Tab1_Frame2)
# use value directly in new Tab#self.master._frame.uid = value#self.master._frame.label['text'] = valuedefnew_window(self):
if self.newWindow:
self.newWindow.destroy()
self.newWindow = tk.Frame(self)
self.uid = self.uidentry.get()
self.app = logwindow(self.newWindow, self.uid)
classTab1_Frame2(tk.Frame):
def__init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.label = tk.Label(self, text="it has been changed!")
self.label.pack()
# get value from master
self.uid = self.master.uid
self.label['text'] = self.uid
# and another button to change it back to the previous frame
self.button = tk.Button(self, text="self", command=lambda: self.master.switch_frame(Tab1_Frame1))
self.button.pack()
defnew_window(self):
self.newWindow = tk.Toplevel(self.master)
self.uid = self.uidentry.get()
if __name__ == "__main__":
root = RootApp()
root.mainloop()
Post a Comment for "How To Pass Tkinter Entry Value From One Frame To Another Through My Switch Frame Function"