2017-12-21 20 views
0

나는 계산기를 연구 중이고 사용자가 첫 번째 숫자를 입력하고 연산자 (+ - */등)를 선택하면 두 번째 숫자를 입력 한 다음 같음 버튼을 눌러 대답을 얻을 수 있기를 바란다. .tkinter 버튼을 사용하여 부울을 업데이트하는 가장 좋은 방법은 무엇입니까?

현재 내 def 안에 내 부울에게 값을 할당 할 수 없기 때문에 두 번째 숫자를 입력하고 결국 함수를 수행 할 수 없습니다. commandButtonPress()의 마지막 줄 것을가 미끄러지는 곳이며, 이전에 list 내가 얻고 있었다 whichNum을 만들기 위해 :

변수를 할당하기 전에 참조

나는 remove()를 사용하여뿐만 아니라 모두 직접 할당 시도 및 append(). 버튼 1-9는 동일한 코드 블록을 사용하며이 문제를 해결하는 데 필요하다고 생각하지 않았기 때문에 십진수를 포함하지 않았습니다.

나는 이것의 어느 것도 우아하지 않으며 클래스를 구현하면 그 대부분이 훨씬 좋을 것이라고 생각하지만, 나는 새로운 것으로 생각하고 실제로 파이썬에서 객체 지향 프로그래밍을하지 않았다.

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() 
+0

[최소한의 완전하고 검증 가능한 예제를 작성하십시오] (https://stackoverflow.com/help/mcve)하십시오. 그것은 당신의 질문이 훨씬 더 간단한 방법으로 요구 될 수 있기 때문에 사람들을 돕는 것을 꺼리는 것입니다. – Nae

+0

모든 함수 밖에서'whichNum = None'을 정의한 다음 모든 함수에'global whichNum'을 첫 번째 라인으로 추가하십시오. – Nae

+0

나는 그것을 파싱했다. 그러나 나는 그것을 너무나 감사 할 것이다. –

답변

0

게시 된 솔루션은 작동하는 것 같습니다. 내 버튼이 num2를 제대로 처리하지 못했지만 commandButtonPress 에서처럼 숫자가 업데이트되지 않습니다. 도와 주셔서 감사합니다. 이 프로젝트를 끝내고 my repo에 업로드했습니다.

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()