Python Forum
[Tkinter] Using variables from one class to another
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Using variables from one class to another
#1
I am new to Tkinter. I am writing a code which has three frames. In the first frame, there is an entry box widget. I want the string entered by the user to be used into the second frame.
I will show my code.

import tkinter as tk
from tkinter import font as tkfont
from tkinter import *

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.title_font = tkfont.Font(family='Futura Bk BT', size=18)
        self.geometry('900x600')
        self.title("Main Heading")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        
        self.frames = {}
        for F in (StartPage, PageOne, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Sub heading", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)

        lbl0 = tk.Label(self, text="Enter number", font=("Futura Bk BT", 12))
        lbl0.place(x=310, y=110, anchor="w")

        txt = tk.Entry(self,width=25)
        txt.place(x=430, y=110, anchor="w")
        txt.focus()
        #OK and Cancel buttons
        btn0 = tk.Button(self, text="OK",
                            command=lambda: controller.show_frame("PageOne"))
        btn0.place(x=410, y=150, anchor="center")
        
        btn1 = tk.Button(self, text="Cancel")
        btn1.place(x=480, y=150, anchor="center")

        btn2 = tk.Button(self, text = "Home",
                            command=lambda: controller.show_frame("StartPage"))     
        btn2.place(x=850, y=30, anchor="center")     

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        lbl1 = tk.Label(self, text="Swapping with Box ID:", font=controller.title_font)
        lbl1.pack(side="top", fill="x", pady=10)

        connection = StringVar()
        connection.set("Connected") 
        lbl = tk.Label(self, text="Connection Status:  %s" % (connection.get()), font=("Futura Bk BT", 8))
        lbl.place(x=20, y=85, anchor="w")

        vno = StringVar()
        vno.set("1234ABCD")
        lbl2 = tk.Label(self, text="number %s " % (vno.get()), font=("Futura Bk BT", 12))
        lbl2.place(x=310, y=150, anchor="w")

        soc = IntVar()
        soc.set("80")
        lbl3 = tk.Label(self, text= "abcd %d %%" %(soc.get()), font=("Futura Bk BT", 12))
        lbl3.place(x=310, y=175, anchor="w")

        energy = DoubleVar()
        energy.set("5.80")
        lbl4 = tk.Label(self, text="efgh %.2f kWh" % (energy.get()), font=("Futura Bk BT", 12))
        lbl4.place(x=310, y=200, anchor="w")


        bill = IntVar()
        bill.set("189")
        lbl5 = tk.Label(self, text="total amount INR %.2f" % (bill.get()), font=("Futura Bk BT", 12))
        lbl5.place(x=310, y=225, anchor="w")

        btn2 = tk.Button(self, text = "Bill Paid",
                            command=lambda: controller.show_frame("PageTwo"))     
        btn2.place(relx = 0.5, rely=0.5, anchor="center")


        btn2 = tk.Button(self, text = "Home",
                            command=lambda: controller.show_frame("StartPage"))     
        btn2.place(x=850, y=30, anchor="center")

class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Swap", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)

        bssLatch = StringVar()
        bssLatch.set("Open")
        lbl5 = tk.Label(self, text=" Latch %s" % (bssLatch.get()), font=("Futura Bk BT", 12))
        lbl5.place(x=310, y=150, anchor="w")

        kitLatch = StringVar()
        kitLatch.set("Open")
        lbl6 = tk.Label(self, text="Kit Latch %s" % (kitLatch.get()), font=("Futura Bk BT", 12))
        lbl6.place(x=310, y=175, anchor="w")

        swap = StringVar()
        swap.set("Open")
        lbl7 = tk.Label(self, text="Swapping Status: %s" % (swap.get()), font=("Futura Bk BT", 12))
        lbl7.place(x=310, y=200, anchor="w")

        btn2 = tk.Button(self, text = "Finish",
                            command=lambda: controller.show_frame("StartPage"))     
        btn2.place(relx = 0.5, rely=0.5, anchor="center")

        btn2 = tk.Button(self, text = "Home",
                            command=lambda: controller.show_frame("StartPage"))     
        btn2.place(x=850, y=30, anchor="center")

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()
I need to get the data entered in txt entry widget in StartFrame to vno in PageOne. How can I achieve this.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020