Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please clarify variables
#1
Question 
Hi guys i need help understanding fundamental uses of variables and functions.
I created a class for a calculator:

class calc:
    def mult(a,b):
        global result  #Declaration must take place in each function. Else its local
        result = a*b
    def div(a,b):
        global result  #Declaration must take place in each function. Else its local
        result=a*b
    def add(a,b):
        global result  #Declaration must take place in each function. Else its local
        result = a*b
    def sub(a,b):
        global result  #Declaration must take place in each function. Else its local
        result = a*b
Then i want to create window with a button using tkinter

from tkinter import *
window = Tk()
window.title('Calculator')
window.geometry("300x200")
entry = Entry(window,text="Enter data")
button = Button(window,text="Click me")
mainloop()
I get no entry nor button on the screen because i cant figure out how to set their position.
This poses another question: How am i even suppose to know what parameters Entry accepts if while typing the code in PyCharm, hint is pretty much useless and gives no examples nor lists accepted parameters ?


Thanks !
Reply
#2
As a fellow rookie, allow me only to suggest taking a step back and figure things out one step at a time. You can't program WOW if you can't program tic-tac-toe.

1) make a working calculator that runs in the terminal.
2) learn how to construct a class.
3) learn tkinter module.

Trying all at once is too much without knowing the vocabulary. Go to youtube and type in "python tutorial". I bet there is even one on how to make a calculator. You can surely also find the code online for one, but following along and typing it yourself will serve you better from a learning standpoint.
Reply
#3
One fundamental uses of variables is: DO NOT USE GLOBAL VARIABLES !
Reply
#4
What you have is not really a class.
OK, technically you use the word class when you define them. But they are in fact just another layer over regular functions. If you remove all the reference to calc your code will work all the same. The idea is that class is blueprint of an object. You can create as many instances of a class as you wish. To be a class it has to have access to instance and/or class itself. Methods would get a class instance as first argument (by convention we use self for that) You really need to get back to read about class basics/fundamentals.
Don't use global variables. You pass arguments to functions/methods and/or use class properties.
Using result property to store well, the result may work, but probably more convenient would be that your methods return the result

So there may be different implementations, but very basic example
class Calculator:
    def __init__(self):
        self._result = 0 # by convention undescore means for internal use variable

    @property
    def result(self):
        return self._result

    def add(self, n):
        self._result += n

    def substract(self, n):
        self._result -= n

    def clear(self):
        self._result = 0

calc = Calculator() # create an instance of the class
print(calc.result)
calc.add(5) # result = 5
print(calc.result)
calc.substract(1) # result = 4
calc.substract(3) # result = 1
print(calc.result)
calc.clear()
print(calc.result)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Could you please clarify where i did mistake also how run without admin right Abubakkar 1 1,793 Jun-14-2021, 09:32 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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