Python Forum
Why is the program not running? Is there a logical or syntax problem?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is the program not running? Is there a logical or syntax problem?
#8
You don't have a textbox. You have an Entry. Do you mean the value entered in self.display? You get the value typed in self.display, but you never pass that along to rule().

You could do this:
    def rule(self, value):
        return 72 / value
 
    def calculate(self):
        x = int(self.display.get())
        result = "Result:   " + str(self.rule(x))
        self.res_label.config(text=result)
I would write the code differently. I am not a fan of making a root window and passing it along to a class. I prefer making the Profit class a subclass of Tk(). That makes Profit the root window. I would also use tkinter variables to get the "display" value and set the res_label.
import tkinter as tk
 
class Profit(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Profit")

        tk.Label(
            self,
            text="Interest rate:"
        ).grid(row=0, column=0)

        self.user_input = tk.IntVar(self, "")
        entry = tk.Entry(
            self,
            textvariable=self.user_input,
            width=10,
            justify=tk.RIGHT,
            font=("Arial", 10)
        )
        entry.grid(row=0, column=1)
        entry.bind("<Return>", self.calculate)
    
        # create the  result field
        self.result = tk.StringVar(self, "Result: ")
        tk.Label(
            self,
            textvariable=self.result
        ).grid(row=1, column=0)
 
    def calculate(self, event):
        self.result.set(f"Result: {72 / self.user_input.get()}")

Profit().mainloop()  #<--  very important.  Notice indentation
behi00 likes this post
Reply


Messages In This Thread
not run yet? - by behi00 - Mar-31-2023, 10:16 PM
RE: PROGRAM MAP - by behi00 - Mar-31-2023, 10:29 PM
RE: Why is the program not running? Is there a logical or syntax problem? - by deanhystad - Mar-31-2023, 10:59 PM
RE: PROGRAM MAP - by behi00 - Mar-31-2023, 10:59 PM
RE: PROGRAM MAP - by behi00 - Mar-31-2023, 11:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] logical error - saving file rwahdan 4 2,218 Jul-01-2021, 12:59 PM
Last Post: rwahdan
  Running cli program within a wx window? t4keheart 2 2,809 Jan-23-2020, 04:50 PM
Last Post: buran
  tkinter GUI, problem running seperate files fishglue 17 6,597 Oct-15-2019, 02:56 PM
Last Post: Denni
  Interacting with python console while program is running Murmele 2 3,384 Feb-10-2018, 05:43 PM
Last Post: kmcollins

Forum Jump:

User Panel Messages

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