from tkinter import * #會造成變數汙染,最好不要這樣用
class Calculator:
def replaceAndEval(self):
text = self.e.get() # “2 + 8 ÷ 2 + 3 x 2”
text = text.replace(“x”, “*”) # “2 + 8 ÷ 2 + 3 * 2”
text = text.replace(“÷”, “/”) # “2 + 8 / 2 + 3 * 2”
result = eval( text )
return result
def equals(self):
result = self.replaceAndEval()
self.allClear()
self.e.insert(0, result)
def square(self):
text = self.replaceAndEval()
result = text ** 2
self.allClear()
self.e.insert(0, result)
def squareRoot(self):
text = self.replaceAndEval()
result = text ** 0.5
self.allClear()
self.e.insert(0, result)
def clearOne(self):
text = self.e.get()[0:-1]
self.allClear()
self.e.insert(0, text)
def allClear(self):
self.e.delete(0, END)
def enterChar(self, symbol):
self.e.insert(END, symbol)
def __init__(self, window):
“”” 參數window只在__init__()
這個function使用到,
沒有使用self.window承接這個參數
__init__() 的縮排內,
就可以只寫window
不用寫self.window
但若class內的其他function
也要用window這個參數
這種寫法就不一定方便,
要把window也傳去別的function “””
self.e = Entry(window, width=28)
self.e.grid(row=0, column=0, columnspan=8)
self.e.focus()
Button(window, text=”=”, width=8, command=self.equals).grid(row=4, column=4, columnspan=2)
Button(window, text=”AC”, width=3, command=self.allClear).grid(row=1, column=4)
Button(window, text=”C”, width=3, command=self.clearOne).grid(row=1, column=5)
Button(window, text=”√”, width=3, command=self.squareRoot).grid(row=3, column=4)
Button(window, text=”x²”, width=3, command=self.square).grid(row=3, column=5)
Button(window, text=”+”, width=3, command=lambda:self.enterChar(“+”) ).grid(row=4, column=3)
Button(window, text=”x”, width=3, command=lambda:self.enterChar(“x”) ).grid(row=2, column=3)
Button(window, text=”-“, width=3, command=lambda:self.enterChar(“-“) ).grid(row=3, column=3)
Button(window, text=”÷”, width=3, command=lambda:self.enterChar(“÷”) ).grid(row=1, column=3)
Button(window, text=”%”, width=3, command=lambda:self.enterChar(“%”) ).grid(row=4, column=2)
Button(window, text=”7″, width=3, command=lambda:self.enterChar(“7”) ).grid(row=1, column=0)
Button(window, text=”8″, width=3, command=lambda:self.enterChar(“8”) ).grid(row=1, column=1)
Button(window, text=”9″, width=3, command=lambda:self.enterChar(“9”) ).grid(row=1, column=2)
Button(window, text=”4″, width=3, command=lambda:self.enterChar(“4”) ).grid(row=2, column=0)
Button(window, text=”5″, width=3, command=lambda:self.enterChar(“5”) ).grid(row=2, column=1)
Button(window, text=”6″, width=3, command=lambda:self.enterChar(“6”) ).grid(row=2, column=2)
Button(window, text=”1″, width=3, command=lambda:self.enterChar(“1”) ).grid(row=3, column=0)
Button(window, text=”2″, width=3, command=lambda:self.enterChar(“2”) ).grid(row=3, column=1)
Button(window, text=”3″, width=3, command=lambda:self.enterChar(“3”) ).grid(row=3, column=2)
Button(window, text=”0″, width=3, command=lambda:self.enterChar(“0”) ).grid(row=4, column=0)
Button(window, text=”.”, width=3, command=lambda:self.enterChar(“.”) ).grid(row=4, column=1)
Button(window, text=”(“, width=3, command=lambda:self.enterChar(“(“) ).grid(row=2, column=4)
Button(window, text=”)”, width=3, command=lambda:self.enterChar(“)”) ).grid(row=2, column=5)
root = Tk()
calc = Calculator(root)
root.mainloop()



推薦hahow線上學習python: https://igrape.net/30afN
![Python: 如何判斷字符串內容是否為數字(整數或浮點數)? isinstance( eval( entry.get() ), (float, int) ) ; str.isdigit() #不包括小數點和負號 ; try~ except ValueError~ ; 正則表示法 regular expression ; pattern = ‘^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$’ Python: 如何判斷字符串內容是否為數字(整數或浮點數)? isinstance( eval( entry.get() ), (float, int) ) ; str.isdigit() #不包括小數點和負號 ; try~ except ValueError~ ; 正則表示法 regular expression ; pattern = ‘^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$’](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2023/05/20230512152430_3.png?quality=90&zoom=2&ssl=1&resize=350%2C233)




![Python如何做excel的樞紐分析? groupbyObj = df.groupby([‘A’, ‘B’]) ; groupbyObj.apply() 跟 groupbyObj.agg() 差異為何? result = groupbyObj .apply( function(df) -> Series ) ; result_agg = groupbyObj .agg( [‘mean’, ‘std’] ) ; aggfunc(Series) -> float Python如何做excel的樞紐分析? groupbyObj = df.groupby([‘A’, ‘B’]) ; groupbyObj.apply() 跟 groupbyObj.agg() 差異為何? result = groupbyObj .apply( function(df) -> Series ) ; result_agg = groupbyObj .agg( [‘mean’, ‘std’] ) ; aggfunc(Series) -> float](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2023/03/20230327140158_46.png?quality=90&zoom=2&ssl=1&resize=350%2C233)


![Python: pandas.DataFrame如何移除所有空白列?if df_raw.iloc[r,0] is np.nan: nanLst.append(r) ; df_drop0 = df_raw.drop(nanLst,axis=0) ; pandas.isna() ;df_drop0 = df_raw.drop(nanLst,axis=0).reset_index(drop=True) Python: pandas.DataFrame如何移除所有空白列?if df_raw.iloc[r,0] is np.nan: nanLst.append(r) ; df_drop0 = df_raw.drop(nanLst,axis=0) ; pandas.isna() ;df_drop0 = df_raw.drop(nanLst,axis=0).reset_index(drop=True)](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2022/12/20221206144233_67.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

近期留言