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?
#1
Hello, I am an amateur programmer.
I wanted to know if my codes are correct?
And why is it not implemented?
Meanwhile, I wrote this program in the object-oriented method and in the tkinter framework.
thank you.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------
import tkinter as tk

class Profit:
    def __init__(self, master):
        self.master = master
        master.title("Profit")
        # create the  pro field
        self.pro_label = tk.Label(master, text= "Interest rate:")
        self.pro_label.grid(row=0, column=0)
        # create the display
        self.display = tk.Entry(master, width=10, justify="center", font=("Arial", 10))
        self.display.grid(row=0, column=1)
        # create the  result field
        self.res_label = tk.Label(master, text="Result:")
        self.res_label.grid(row=1, column=0)
        # create and place the buttons on the grid
        self.calculate_button = tk.Button(master,text="Computing",command=self.calculate)
        self.calculate_button.grid(row=2, column=0)
      def rule(master):
      return 72/master

    def calculate(self):
                x = int(self.display.get())
                result = "Result:   " + self.rule()
                self.res_label.config(text=result)
# create the main window and profit object
 root = tk.Tk()
 pro = Profit(root)
 root.mainloop()
Gribouillis write Mar-31-2023, 09:04 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
You have some indentation errors. For example:
      def rule(master):
      return 72/master
rule() is not declared to be a static method. Either you need a @staticmethod decorator or add self as the first argument (def rule(self, master). I'm not sure which is correct, because rule() doesn't make any sense. What is "master" supposed to be?
Reply
#3
C:\Users\behnam_moein\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\behnam_moein\PycharmProjects\pythonProject\main.py
File "C:\Users\behnam_moein\PycharmProjects\pythonProject\main.py", line 19
def rule(master):
^
IndentationError: unindent does not match any outer indentation level

Process finished with exit code 1

------------------------------------------------------------------------------------------------------
Above we have the errors resulting from the execution of the program.
Reply
#4
Your indentation is all over the place. This is correct:
import tkinter as tk
 
class Profit:
    def __init__(self, master):
This is wrong:
        self.calculate_button.grid(row=2, column=0)
      def rule(master):   #<- Should be same indentation as __init__.  Should also be blank line before def rule(master):
      return 72/master
# Should be indented 4 space from line above.

And this is wrong:
# create the main window and profit object
 root = tk.Tk()     # <- Should be no spaces, not 1
 pro = Profit(root)
 root.mainloop()
Your rule method is not written correctly (see my first post). It is likely you'll need to change the code where you call the method too.
Reply
#5
thanks for your helps but not run yet?
why??!! i don't know.
import tkinter as tk

class Profit:
    def __init__(self, master):
        self.master = master
        master.title("Profit")
        # create the  pro field
        self.pro_label = tk.Label(master, text= "Interest rate:")
        self.pro_label.grid(row=0, column=0)
        # create the display
        self.display = tk.Entry(master, width=10, justify="center", font=("Arial", 10))
        self.display.grid(row=0, column=1)
        # create the  result field
        self.res_label = tk.Label(master, text="Result:")
        self.res_label.grid(row=1, column=0)
        # create and place the buttons on the grid
        self.calculate_button = tk.Button(master,text="Computing",command=self.calculate)
        self.calculate_button.grid(row=2, column=0)

    def rule(master):
          return 72/master

    def calculate(self):
        x = int(self.display.get())
        result = "Result:   " + self.rule()
        self.res_label.config(text=result)
        # create the main window and profit object
        root = tk.Tk()
        pro = Profit(root)
        root.mainloop()
Reply
#6
Use the Python button in the editor to wrap your code in Python tags.

You still have an error in root().

And you have a new indentation error:
        self.res_label.config(text=result)
        # create the main window and profit object
        # create the main window and profit object
        root = tk.Tk()
        pro = Profit(root)
        root.mainloop()
As currently written, you are making root inside the calculate() method.

And this indentation is also wrong, though it probably doesn't stop your code running.
    def rule(master):
          return 72/master
The rest of your code uses 4 space indents. This has 8 spaces. Be consistent.
Reply
#7
In fact, the RULE function is supposed to take the value entered in the TEXTBOX and divide it by 72, and the result of this division is used in the CALCULATE function.
TAHNKS AGAIN.
Reply
#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
#9
I changed the things you said in the last post, which were all correct, but the form is still not displayed for me.



import tkinter as tk

class Profit:
    def __init__(self, master):
        self.master = master
        master.title("Profit")
        # create the  pro field
        self.pro_label = tk.Label(master, text= "Interest rate:")
        self.pro_label.grid(row=0, column=0)
        # create the display
        self.display = tk.Entry(master, width=10, justify="center", font=("Arial", 10))
        self.display.grid(row=0, column=1)
        # create the  result field
        self.res_label = tk.Label(master, text="Result:")
        self.res_label.grid(row=1, column=0)
        # create and place the buttons on the grid
        self.calculate_button = tk.Button(master,text="Computing",command=self.calculate)
        self.calculate_button.grid(row=2, column=0)

    def rule(self , value):
        return 72/value

    def calculate(self):
        x = int(self.display.get())
        result = "Result:   " + self.rule(x)
        self.res_label.config(text=result)
        # create the main window and profit object
        root = tk.Tk()
        pro = Profit(root)
        root.mainloop()
Reply
#10
I tried to run your code in pycharm but failed.
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()}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] logical error - saving file rwahdan 4 2,105 Jul-01-2021, 12:59 PM
Last Post: rwahdan
  Running cli program within a wx window? t4keheart 2 2,757 Jan-23-2020, 04:50 PM
Last Post: buran
  tkinter GUI, problem running seperate files fishglue 17 6,345 Oct-15-2019, 02:56 PM
Last Post: Denni
  Interacting with python console while program is running Murmele 2 3,329 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