私は電卓に取り組んでおり、ユーザーが最初の数値の入力を完了して演算子(+-* /など)を選択したら、2番目の数値を入力し、等しいボタンを押して答えを取得できるようにしたいと考えています。
現在、def
s内のboolに値を割り当てることができないため、2番目の数値を入力して、最終的に関数を実行することができません。最後の行は、commandButtonPress()
物事がアップ滑り、そして作る前にしているところである私がなっていました。whichNum
list
割り当て前に参照される変数
直接割り当てremove()
とandの使用の両方を試しましたappend()
。ボタン1〜9は同じコードブロックを使用しますが、この問題を解決するために必要だとは思わなかったため、小数は含めませんでした。
私はこれのどれもエレガントではなく、クラスを実装した方がはるかに良いことを知っていますが、私は新しく、物事を理解しようとしていて、Pythonでオブジェクト指向プログラミングをまだ行っていません。
from tkinter import *
def buttonOne(whichNum):
count = 0
number = ""
try:
if whichNum == False and num1[0] != "0":
num1.append("1")
while count < len(num1):
number = number + str(num1[count])
count += 1
screen["text"] = number
elif whichNum == True and num2[0] != "0":
num2.append("1")
while count < len(num2):
number = number + str(num2[count])
count += 1
screen["text"] = number
except:
if whichNum == False:
num1[0] = "."
else:
num2[0] = "."
number = "0."
screen["text"] = number
def commandButtonPress(symbol, whichNum):
if whichNum == False:
if len(operator) > 0 and operator[0] == "+":
operator.remove("+")
if len(num1) > 0:
operator.append(symbol)
whichNum[0] = True
def main():
window = Tk()
window.title("Calculator")
window.geometry("250x305")
num1 = []
num2 = []
operator = []
whichNum = [False]
global screen
screen = Label(text="0", height = 5, width = 30)
screen.pack(side = LEFT, expand=False, anchor = N, padx=[5, 5])
button1 = Button(text="1", command = lambda: buttonOne(whichNum[0]))
button1.grid(column=0, row=2)
button11 = Button(text="+", command = lambda: commandButtonPress("+", whichNum[0]))
button11.grid(column=3, row=2)
window.mainloop()
main()
Naeが投稿したソリューションは機能しているようです。私のボタンはまだnum2を適切に処理していませんが、commandButtonPressで必要なようにwhichNumが更新されています。助けてくれてありがとう。私はこのプロジェクトを(多かれ少なかれ)終了し、自分のリポジトリにアップロードしました。
from tkinter import *
whichNum = False
def buttonOne():
global whichNum
count = 0
number = ""
try:
if whichNum == False and num1[0] != "0":
num1.append("1")
while count < len(num1):
number = number + str(num1[count])
count += 1
screen["text"] = number
elif whichNum == True and num2[0] != "0":
num2.append("1")
while count < len(num2):
number = number + str(num2[count])
count += 1
screen["text"] = number
except:
if whichNum == False:
num1[0] = "."
else:
num2[0] = "."
number = "0."
screen["text"] = number
def commandButtonPress(symbol):
global whichNum
if whichNum == False:
if len(operator) > 0 and operator[0] == "+":
operator.remove("+")
if len(num1) > 0:
operator.append(symbol)
whichNum[0] = True
def main():
window = Tk()
window.title("Calculator")
window.geometry("250x305")
num1 = []
num2 = []
operator = []
global whichNum
global screen
screen = Label(text="0", height = 5, width = 30)
screen.pack(side = LEFT, expand=False, anchor = N, padx=[5, 5])
button1 = Button(text="1", command = lambda: buttonOne())
button1.grid(column=0, row=2)
button11 = Button(text="+", command = lambda: commandButtonPress("+"))
button11.grid(column=3, row=2)
window.mainloop()
main()
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加