Python Forum
how to add two numbers and pass the result to the next page in tkinter?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to add two numbers and pass the result to the next page in tkinter?
#4
You don't need a lambda expression for something like this:
command=lambda: self.go_to_page_two()
Instead use:
command=self.go_go_page_two
Only use a lambda if you want to specify arguments for the method call.

In this code you are creating a string by concatenating the strings in entry1 and entry2.
self.controller.SomeVar = self.entry1.get() + self.entry2.get()
I do not like Page1 having to know about things in Page2. For something small like this it is ok, but if you had several windows that share information, this way of keeping everything current quickly becomes unmaintainable. A better design splits out all the information you are working with into it's own entity. Frame 1 would change a value in this data entity. If Frame 2 has a display for that data, registers a function to call when the value of the data changes.
import tkinter as tk

class Data:
    """I an a data entity shared by the forms"""
    def __init__(self):
        self.first_no = tk.IntVar()
        self.second_no = tk.IntVar()
        self.someVar = tk.IntVar()


class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.title("Summation App")
        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")
  
        # I knob about all the frames, so it is ok for me to reference parts of their API
        self.frames[PageOne].page_button.config(command=self.go_to_page_two)
        self.show_frame(PageOne)

    def go_to_page_two(self):
        # I do not update the the label.  There are limits
        self.data.someVar.set(self.data.first_no.get() + self.data.second_no.get())
        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
  
        self.frame1 = tk.LabelFrame(self, text="This is page one")
        self.frame1.pack(padx=10, pady=10)
  
        label1 = tk.Label(self.frame1, text= "First No.")
        label1.grid(row=0, column=0)
  
        label2 = tk.Label(self.frame1, text= "Second No.")
        label2.grid(row=1, column=0)
  
        self.entry1 = tk.Entry(self.frame1, textvariable=data.first_no)
        self.entry1.grid(row=0, column=1)
  
        self.entry2 = tk.Entry(self.frame1, textvariable=data.second_no)
        self.entry2.grid(row=1, column=1)
  
        self.page_button = tk.Button(self.frame1, text="Go to Page Two")
        self.page_button.grid(row=4, column=0, padx=10, pady=10)
  
  
class PageTwo(tk.Frame):
    def __init__(self, parent, data):
        super().__init__(parent)
        self.data = data
  
        self.frame2 = tk.LabelFrame(self, text="This is page two")
        self.frame2.pack()
  
        self.label3 = tk.Label(self.frame2)
        self.label3.pack()

        # When someone changes someVar I need to update the label
        data.someVar.trace('w', lambda a, b, c: self.correct_label())
  
    def correct_label(self):
        self.label3.config(text=self.data.someVar.get())
  
  
app = SampleApp()
app.mainloop()
pymn likes this post
Reply


Messages In This Thread
RE: how to add two numbers and pass the result to the next page in tkinter? - by deanhystad - Feb-14-2022, 05:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pass a variable between tkinter and toplevel windows janeik 10 3,073 Jan-24-2024, 06:44 AM
Last Post: Liliana
  [Tkinter] How to get the result of a ping to show in tkinter? jacklee26 6 8,136 Feb-10-2023, 01:12 PM
Last Post: NebularNerd
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 5,248 Nov-17-2021, 03:21 AM
Last Post: deanhystad
Thumbs Up tkinter canvas; different page sizes on different platforms? philipbergwerf 4 4,438 Mar-27-2021, 05:04 AM
Last Post: deanhystad
Star [Tkinter] How to perform math function in different page of Tkinter GUI ravaru 2 4,773 Oct-23-2020, 05:46 PM
Last Post: deanhystad
  [Tkinter] tkinter How to pass label fiilename to another module? johnjh 0 2,094 Apr-17-2020, 11:34 PM
Last Post: johnjh
  Tkinter: increasing numbers and Radiobutton issue PeroPuri 1 2,279 Apr-13-2020, 05:48 PM
Last Post: deanhystad
  Want to dynamically update numbers using tkinter in pygame script k0gane 0 2,150 Feb-09-2020, 09:01 AM
Last Post: k0gane
  [Tkinter] Result not change using Tkinter cmala 2 2,865 May-17-2019, 08:12 AM
Last Post: cmala

Forum Jump:

User Panel Messages

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