Python Forum
Why is the program not running? Is there a logical or syntax problem? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Why is the program not running? Is there a logical or syntax problem? (/thread-39717.html)

Pages: 1 2


Why is the program not running? Is there a logical or syntax problem? - behi00 - Mar-31-2023

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



RE: Why is the program not running? Is there a logical or syntax problem? - deanhystad - Mar-31-2023

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?


RE: Why is the program not running? Is there a logical or syntax problem? - behi00 - Mar-31-2023

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.


RE: Why is the program not running? Is there a logical or syntax problem? - deanhystad - Mar-31-2023

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.


not run yet? - behi00 - Mar-31-2023

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



RE: Why is the program not running? Is there a logical or syntax problem? - deanhystad - Mar-31-2023

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.


RE: PROGRAM MAP - behi00 - Mar-31-2023

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.


RE: Why is the program not running? Is there a logical or syntax problem? - deanhystad - Mar-31-2023

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



RE: PROGRAM MAP - behi00 - Mar-31-2023

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



RE: PROGRAM MAP - behi00 - Mar-31-2023

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