Hands-level computers, come and fight monsters.
<Python06 calculator-knowledge point
Import tkinterclass jsq: # interface Layout MethodDef _ init _ (self): self. root = tkinter. tk () self. root. minsize (300,450) self. root. maxsize (300,450) self. root. title ('calculator') # variable # store the number and symbol variable self. lists = [] # It is assumed that the key is wrong self. isPressSign = False # The initialization page number is 0 self. res = tkinter. stringVar () self. res. set (0) # input into the first line to include the case of symbol (self. res2 = tkinter. stringVar () # UI layout self. xsmb () self. caidan () self. root. mainloop () def caidan (self): # create the total menu menubar = tkinter. menu (self. root) # create a drop-down menu and add the File menu filemenu = tkinter. menu (menubar) # create the option filemenu to view the Menu. add_command (label = "open", command = self. caidan) filemenu. add_command (label = "Storage", command = self. caidan) # create the separation line filemenu of the drop-down menu. add_separator () filemenu. add_command (label = "close", command = self. root. quit) # Add the File menu to the total menu as a drop-down menu and name it File menubar. add_cascade (label = "menu", menu = filemenu) # create an editing menu editmenu = tkinter. menu (menubar, tearoff = 0) # create and edit the Menu option editmenu. add_command (label = 'Copy (C) ', command = self. caidan) editmenu. add_command (label = 'paste (P) ', command = self. caidan) menubar. add_cascade (label = "edit", menu = editmenu) # display the total menu self. root. config (menu = menubar) # press the number operation method def szbtn (self, num): # press the symbol key to output 0 if self. isPressSign = False: pass else: self. res. set (0) # assign a value again and press the symbol key to false self. isPressSign = False # The input value is oldnum = self. res. get () # determine whether the initial number is 0. if oldnum = '0': self. res. set (num) else: newnum = oldnum + num self. res. set (newnum) # press the symbol key operation method def pressCompute (self, sign): # Save the input number and the number to the list num = self. res. get () self. lists. append (num) self. lists. append (sign) # backend key function if sign = '<--': l = num [0:-1] self. res. set (l) # determine whether the input character is the clear key if sign = 'C': self. lists. clear () self. res. set (0) # if sign = '√ ': # self. res. set (math. sqrt (num) # reset the button to true self. isPressSign = True # output computation result def pressEqual (self): # Add the number that is pressed by the following operator to the list. curnum = self. res. get () self. lists. append (curnum) # press the equal sign to trigger the computeStr = ''. join (self. lists) endnum = eval (computeStr) # output the stored operation process to the first text box self. res2.set (computeStr) # output result to clear the list self. res. set (endnum) self. lists. clear ()# Layout interface text box and button method def xsmb (self): lable = tkinter. label (self. root, textvariable = self. res, bg = 'white', font = ('simhei ', 20), anchor = 'E') lable. place (x = 20, y = 20, width = 260, height = 60) lable2 = tkinter. label (self. root, textvariable = self. res2, bg = 'blue', font = (' ', 10), anchor = 'E') lable2.place (x = 20, y = 20, width = 260, height = 20) # The first line of symbol key btnyichu = tkinter. button (self. root, text = '<--', font = ('simhei ', 20), command = lambda: self. pressCompute ('<--') btnyichu. place (x = 20, y = 100, width = 95, height = 40) btnclear = tkinter. button (self. root, text = 'C', font = ('simhei ', 20), command = lambda: self. pressCompute ('C') btnclear. place (x = 130, y = 100, width = 40, height = 40) # btnsqrt = tkinter. button (self. root, text = '√ ', font = ('simhei', 20), command = lambda: self. pressCompute ('√ ') # btnsqrt. place (x = 185, y = 100, width = 40, height = 40) btnmiyunsuan = tkinter. button (self. root, text = '**', font = ('simhei ', 20), command = lambda: self. pressCompute ('**') btnmiyunsuan. place (x = 240, y = 100, width = 40, height = 40) # The first line of the number key btn1 = tkinter. button (self. root, text = '1', font = ('simhei ', 20), command = lambda: self. szbtn ('1') btn1.place (x = 20, y = 160, width = 40, height = 40) btn2 = tkinter. button (self. root, text = '2', font = ('simhei ', 20), command = lambda: self. szbtn ('2') btn2.place (x = 75, y = 160, width = 40, height = 40) btn3 = tkinter. button (self. root, text = '3', font = ('simhei ', 20), command = lambda: self. szbtn ('3') btn3.place (x = 130, y = 160, width = 40, height = 40) # Second line symbol key, division and floor Division except btnchu = tkinter. button (self. root, text = '/', font = ('simhei ', 20), command = lambda: self. pressCompute ('/') btnchu. place (x = 185, y = 160, width = 40, height = 40) btndibanchu = tkinter. button (self. root, text = '//', font = ('simhei ', 20), command = lambda: self. pressCompute ('//') btndibanchu. place (x = 240, y = 160, width = 40, height = 40) # The second row of the number key btn4 = tkinter. button (self. root, text = '4', font = ('simhei ', 20), command = lambda: self. szbtn ('4') btn4.place (x = 20, y = 220, width = 40, height = 40) btn5 = tkinter. button (self. root, text = '5', font = ('simhei ', 20), command = lambda: self. szbtn ('5') btn5.place (x = 75, y = 220, width = 40, height = 40) btn6 = tkinter. button (self. root, text = '6', font = ('simhei ', 20), command = lambda: self. szbtn ('6') btn6.place (x = 130, y = 220, width = 40, height = 40) # symbol of the third line, multiplication, obtained in btncheng = tkinter. button (self. root, text = '*', font = ('simhei ', 20), command = lambda: self. pressCompute ('*') btncheng. place (x = 185, y = 220, width = 40, height = 40) btnquyu = tkinter. button (self. root, text = '%', font = ('simhei ', 20), command = lambda: self. pressCompute ('%') btnquyu. place (x = 240, y = 220, width = 40, height = 40) btn7 = tkinter. button (self. root, text = '7', font = ('simhei ', 20), command = lambda: self. szbtn ('7') btn7.place (x = 20, y = 280, width = 40, height = 40) btn8 = tkinter. button (self. root, text = '8', font = ('simhei ', 20), command = lambda: self. szbtn ('8') btn8.place (x = 75, y = 280, width = 40, height = 40) btn9 = tkinter. button (self. root, text = '9', font = ('simhei ', 20), command = lambda: self. szbtn ('9') btn9.place (x = 130, y = 280, width = 40, height = 40) btnjian = tkinter. button (self. root, text = '-', font = ('simhei ', 20), command = lambda: self. pressCompute ('-') btnjian. place (x = 185, y = 280, width = 40, height = 40) btn0 = tkinter. button (self. root, text = '0', font = ('simhei ', 20), command = lambda: self. szbtn ('0') btn0.place (x = 20, y = 340, width = 95, height = 40) btndian = tkinter. button (self. root, text = '. ', font = ('simhei', 20), command = lambda: self. szbtn ('. ') btndian. place (x = 130, y = 340, width = 40, height = 40) btnjia = tkinter. button (self. root, text = '+', font = ('simhei ', 20), command = lambda: self. pressCompute ('+') btnjia. place (x = 185, y = 340, width = 40, height = 40) btndeng = tkinter. button (self. root, text = ', font = ('simhei', 20), command = lambda: self. pressEqual () btndeng. place (x = 240, y = 280, width = 40, height = 100) jsq ()
It wasn't long before you learned Python, and there wasn't much water for you. After a day or two, the computer was rough. The menu is basically a waste of/(too o workflow )/~~...... The functions are still square open, but I don't know what to expect.
The logic is based on the input number-input operator-input number-'=.
There are also multiple bugs: it is known that the output result is directly added to the result after the 1 operation is completed.
2. Pressing the equal sign key multiple times can only be performed once. You cannot directly use the equal sign to perform multiple operations.
3. the text box in the first row of records is not uploaded from the start of input, and the operation ends. You cannot press the key to clearly understand the user experience.
.....
Sorry for the poor operation because many other functions are not implemented.
Python Learning Resource Sharing group: QQ 563626388
>