I want to obtain the current entry value from the Page2
of the Tkinter app when the button Run
is clicked.
Please help!
My codes:
import tkinter as tk
from tkinter import ttk
from tkinter import RAISED
LARGEFONT =("Verdana", 35)
class tkinterApp(tk.Tk):
# __init__ function for class tkinterApp
def __init__(self, *args, **kwargs):
# __init__ function for class Tk
tk.Tk.__init__(self, *args, **kwargs)
# creating a container
container = tk.Frame(self)
container.pack(side = "top", fill = "both", expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
container2 = tk.Frame(self, relief=RAISED, borderwidth=2)
container2.pack(side="bottom",fill="both", expand=True)
container2.grid_rowconfigure(0, weight = 1)
container2.grid_columnconfigure(0, weight = 1)
def runStadiumpy():
pass
#get all entry value from page 2
runButton = ttk.Button(container2, text="Run", command=runStadiumpy)
runButton.pack(side="left", padx=5, pady=5)
# initializing frames to an empty array
self.frames = {}
# iterating through a tuple consisting
# of the different page layouts
for F in (StartPage, Page1, Page2):
frame = F(container, self)
# initializing frame of that object from
# startpage, page1, page2 respectively with
# for loop
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky ="nsew")
self.show_frame(StartPage)
# to display the current frame passed as
# parameter
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
# first window frame startpage
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
# label of frame Layout 2
label = ttk.Label(self, text ="Startpage", font = LARGEFONT)
# putting the grid in its place by using
# grid
label.grid(row = 0, column = 2, padx = 10, pady = 10)
button1 = ttk.Button(self, text ="Page 1",
command = lambda : controller.show_frame(Page1))
# putting the button in its place by
# using grid
button1.grid(row = 1, column = 1, padx = 10, pady = 10)
## button to show frame 2 with text layout2
button2 = ttk.Button(self, text ="Page 2",
command = lambda : controller.show_frame(Page2))
# putting the button in its place by
# using grid
button2.grid(row = 2, column = 1, padx = 10, pady = 10)
# second window frame page1
class Page1(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = ttk.Label(self, text ="Page 1", font = LARGEFONT)
label.grid(row = 0, column = 2, padx = 10, pady = 10)
# button to show frame 2 with text
# layout2
button1 = ttk.Button(self, text ="StartPage",
command = lambda : controller.show_frame(StartPage))
# putting the button in its place
# by using grid
button1.grid(row = 1, column = 1, padx = 10, pady = 10)
# button to show frame 2 with text
# layout2
button2 = ttk.Button(self, text ="Page 2",
command = lambda : controller.show_frame(Page2))
# putting the button in its place by
# using grid
button2.grid(row = 2, column = 1, padx = 10, pady = 10)
# third window frame page2
class Page2(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = ttk.Label(self, text ="Page 2", font = LARGEFONT)
label.grid(row = 0, column = 2, padx = 10, pady = 10)
# button to show frame 2 with text
# layout2
button1 = ttk.Button(self, text ="Page 1",
command = lambda : controller.show_frame(Page1))
# putting the button in its place by
# using grid
button1.grid(row = 1, column = 1, padx = 10, pady = 10)
# button to show frame 3 with text
# layout3
button2 = ttk.Button(self, text ="Startpage",
command = lambda : controller.show_frame(StartPage))
# putting the button in its place by
# using grid
button2.grid(row = 2, column = 1, padx = 10, pady = 10)
lbl1 = ttk.Label(self, text="Question:")
lbl1.grid(row = 3, column = 1, padx = 10, pady = 10)
entry1 = ttk.Entry(self)
entry1.grid(row = 3, column = 2, padx = 10, pady = 10)
entry1.insert(0,"abc")
# Driver Code
app = tkinterApp()
app.mainloop()
Keep a reference for your entry in class Page2
, like:
class Page2(tk.Frame):
def __init__(self, parent, controller):
......
self.entry1 = ttk.Entry(self)
self.entry1.grid(row = 3, column = 2, padx = 10, pady = 10)
self.entry1.insert(0,"abc")
Due to you save those frames in your self.frames
, you could visit Page2
by self.frames[Page2]
.After add a reference for your entry, use self.frames[Page2].entry1
to get the entry directly.
So after add the code above, use this code below in function runStadiumpy()
:
def runStadiumpy():
print(self.frames[Page2].entry1.get())
And you will get the content in the Page2