Καλησπέρα!
Ανοίγω εδώ νέο θέμα επειδή πιστεύω πως είναι ενδιαφέρον από μόνο του. Αν κάνω λάθος τοποθετήστε όπως κρίνετε.
Ο λόγος για το calculator, που προσθέσατε στον οδηγό, (πολύ καλός οδηγός ! Παιδιά ευχαριστούμε -τα πρωτάκια στην python
- !)
To calculator είναι ότι πρέπει για μελέτη και μπορείτε να το κατεβάζετε από
εδώ.
Να πω ότι θέλει λίγο δουλίτσα, (επειδή είναι pdf ίσως; Δεν ξέρω) ο κώδικας όταν αντιγραφτεί
στον editor. Έβγαζε τα ονόματα των συναρτήσεων με κενά ανάμεσα στα γράμματα.
Το διόρθωσα όσο μπορώ κι όσο ξέρω.
Τρέχοντας το πρόγραμμα (το ονόμασα calculator.py), μου έβγαλε αυτό το μύνημα
$ python3 calcula
tor.py
Traceback (most recent call last):
File "calculator.py", line 113, in <module>
calc = Calculator(master = root)
File "calculator.py", line 111, in __init__
self.drawGUI()
File "calculator.py", line 83, in drawGUI
self.insertNum()
AttributeError: 'Calculator' object has no attribute 'insertNum'
Ο κώδικας όπως τον έχω κάνει είναι :
from tkinter import *
from tkinter.messagebox import *
class Calculator(Frame):
""" Describes the main behaviour o f a c a l c u l a t o r
"""
def d_result(self, c):
""" Displays the r e s u l t o f an operation
"""
self.label3.config(text = 'The result is ' + str(c))
self.label3.pack()
def d_add(self):
""" Handles the addition in the GUI
"""
a, b = self.getNum()
c = self.addition(a, b)
self.d_result(c)
def d_sub(self):
""" Handles the substraction in the GUI
"""
a, b = self.getNum()
c = self.substraction(a, b)
self.d_result(c)
def d_mul(self):
""" Handles the m u l t i p l i c a t i o n in the GUI
"""
a, b = self.getNum()
c = self.multiplication(a, b)
self.d_result(c)
def d_div(self):
""" Handles the d i v i s i o n in the GUI
"""
a, b = self.getNum()
c = self.division(a, b)
self.d_result(c)
def addition(self, a, b):
""" Add numbers a , b and return t h e i r r e s u l t
"""
c = a + b
return c
def substraction(self, a, b):
""" Substract a − b and return the r e s u l t
"""
c = a - b
return c
def multiplication(self, a, b):
""" Multiply a ∗ b and return i t s r e s u l t
"""
c = a * b
return c
def division(self, a, b):
""" Divide a / b and return i t s r e s u l t
"""
try:
if b != 0:
c = a/b
except ZeroDivisionError:
if b == 0:
showinfo('Warning ', ' Cannot divide by 0.')
c = 0
return c
def getNum(self):
""" Gets the two numbers on which the operation
w i l l be applied.
In case o f error , i t returns both o f them as zero
and warns the user.
"""
try:
a = float(self.enter1.get())
b = float(self.enter2.get())
#if the f i e l d s were empty
except ValueError :
a, b = 0, 0
showinfo('Info', 'There are some empty fields')
return a, b
def drawGUI(self):
"""
I t draws the GUI within the container from which i s
c a l l e d . In t h i s case , within the frame where the
c a l c u l a t o r belongs.
"""
# draws the elements which are used t o i n s e r t a number
self.insertNum()
# set the focus t o enter1
self.enter1.focus()
# create the buttons
self.button1 = \
Button(self, text = "add", command = self.d_add)
self.button2 = \
Button(self, text = "sub", command = self.d_sub)
self.button3 = \
Button(self, text = "mul", command = self.d_mul)
self.button4 = \
Button(self, text = "div", command = self.d_div)
# display them
self.button1.pack(side=LEFT)
self.button2.pack(side=LEFT)
self.button3.pack(side=LEFT)
self.button4.pack(side=LEFT)
# create the l a b e l where we display the r e s u l t
self.label3 = Label(self)
return root;
def __init__(self, master = None):
"""
The constructor i s c a l l e d with a parent widget .
I t creates the GUI f o r the c a l c u l a t o r .
"""
Frame.__init__(self, master)
self.master.title("Calculator")
self.pack()
self.drawGUI()
root = Tk()
calc = Calculator(master = root)
calc.mainloop()
Ευχαριστώ !
Υ.Γ. Αποσχολίασα τις γραμμές 54 και 57 γιατί έτσι νόμιζα ότι πρέπει.