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?
#8
(Feb-14-2022, 08:17 PM)deanhystad Wrote: It is not good user interface design to have controls in one "page" change displays in a second "page". It is not an absolute, but should give you pause. If you don't immediately see the results of your actions, how do you know that you performed the action correctly? For your example I would put a button in frame 2 that draws a dialog to enter values for first_no and second_no which are applied when you press the "Ok" button in the dialog, or ignored if you press the "Cancel" button.

I cannot tell you what is a good design for your application (I don't know what you are trying to achieve), but I can say you should be spending most of your programming time thinking about the user experience and relatively little time writing code.

I am still working on this problem. I am so confused. could you please help me with that. imagine I have a text file with a few numbers in that, and I want to pass it as a string to the next page. I updated my code. I created a new attribute in Data class as a string (new_Var). I cannot figure out how to make the connection between my opentext() function and the new_Var.

import tkinter as tk
from tkinter import filedialog

class Data:

    def __init__(self):
        self.first_no = tk.IntVar()
        self.second_no = tk.IntVar()
        self.summ = tk.IntVar()
        self.new_Var = tk.StringVar()


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")

        self.frames[PageOne].page_button.config(command=self.go_to_page_two)
        self.show_frame(PageOne)

    def go_to_page_two(self):
        self.data.summ.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

        frame1 = tk.LabelFrame(self, text="This is page one")
        frame1.pack(padx=10, pady=10)

        label1 = tk.Label(frame1, text="First No.")
        label1.grid(row=0, column=0)

        label2 = tk.Label(frame1, text="Second No.")
        label2.grid(row=1, column=0)

        self.entry1 = tk.Entry(frame1, textvariable=data.first_no)
        self.entry1.grid(row=0, column=1)

        self.entry2 = tk.Entry(frame1, textvariable=data.second_no)
        self.entry2.grid(row=1, column=1)

        self.page_button = tk.Button(frame1, text="Go to Page Two")
        self.page_button.grid(row=3, column=0, padx=10, pady=10)

        label3 = tk.Label(frame1, text="Import the file")
        label3.grid(row=2, column=0, padx=10)

        def opentext():
            my_file = filedialog.askopenfilenames(initialdir="/pycharm", title="Select your file")
            for T in my_file:
                with open(T, 'r') as infile1:
                    lines = infile1.read()
                    label4.config(text=lines)
                    return lines

        self.button1 = tk.Button(frame1, text="Open file(s)", command=opentext)
        self.button1.grid(row=2, column=1, padx=10, pady=10)

        label4 = tk.Label(frame1)
        label4.grid(row=3, column=1)

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.label5 = tk.Label(self.frame2)
        self.label5.pack()

        self.label6 = tk.Label(self.frame2)
        self.label6.pack()

        # When someone changes summ, I need to update the label
        data.summ.trace('w', lambda a, b, c: self.correct_label())

    def correct_label(self):
        self.label5.config(text=self.data.summ.get())
        self.label6.config(text=self.data.new_Var)


app = SampleApp()
app.mainloop()
Reply


Messages In This Thread
RE: how to add two numbers and pass the result to the next page in tkinter? - by pymn - Feb-15-2022, 04:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pass a variable between tkinter and toplevel windows janeik 10 2,538 Jan-24-2024, 06:44 AM
Last Post: Liliana
  [Tkinter] How to get the result of a ping to show in tkinter? jacklee26 6 7,954 Feb-10-2023, 01:12 PM
Last Post: NebularNerd
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 4,977 Nov-17-2021, 03:21 AM
Last Post: deanhystad
Thumbs Up tkinter canvas; different page sizes on different platforms? philipbergwerf 4 4,236 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,649 Oct-23-2020, 05:46 PM
Last Post: deanhystad
  [Tkinter] tkinter How to pass label fiilename to another module? johnjh 0 2,037 Apr-17-2020, 11:34 PM
Last Post: johnjh
  Tkinter: increasing numbers and Radiobutton issue PeroPuri 1 2,197 Apr-13-2020, 05:48 PM
Last Post: deanhystad
  Want to dynamically update numbers using tkinter in pygame script k0gane 0 2,107 Feb-09-2020, 09:01 AM
Last Post: k0gane
  [Tkinter] Result not change using Tkinter cmala 2 2,792 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