Posts: 6
Threads: 1
Joined: Mar 2023
Mar-31-2023, 08:52 PM
(This post was last modified: Mar-31-2023, 09:04 PM by Gribouillis.)
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()
Posts: 6,196
Threads: 16
Joined: Feb 2020
Mar-31-2023, 09:45 PM
(This post was last modified: Mar-31-2023, 09:45 PM by deanhystad.)
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?
Posts: 6
Threads: 1
Joined: Mar 2023
Mar-31-2023, 09:51 PM
(This post was last modified: Mar-31-2023, 09:58 PM by behi00.)
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.
Posts: 6,196
Threads: 16
Joined: Feb 2020
Mar-31-2023, 09:58 PM
(This post was last modified: Mar-31-2023, 09:58 PM by deanhystad.)
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.
Posts: 6
Threads: 1
Joined: Mar 2023
Mar-31-2023, 10:16 PM
(This post was last modified: Mar-31-2023, 10:26 PM by deanhystad.)
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()
Posts: 6,196
Threads: 16
Joined: Feb 2020
Mar-31-2023, 10:28 PM
(This post was last modified: Mar-31-2023, 10:31 PM by deanhystad.)
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.
Posts: 6
Threads: 1
Joined: Mar 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.
Posts: 6,196
Threads: 16
Joined: Feb 2020
Mar-31-2023, 10:59 PM
(This post was last modified: Apr-01-2023, 02:32 AM by deanhystad.)
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
Posts: 6
Threads: 1
Joined: Mar 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()
Posts: 6
Threads: 1
Joined: Mar 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()}")
|