2017-11-03 1 views
1

다른 사람들의 코드를 기반으로 동일한 문제를 시도했지만 시도 할 수 없었습니다.Tkinter - 정수 입력 값을 얻는 방법?

여기서 문제는 calculate 기능을 수행하기 전에 코드가 Entry 상자의 값을 가져 오지 않는다는 것입니다.

코드를 실행했기 때문에이 버전을 올렸지 만 시도한 다른 방법은 오류 메시지를 표시했습니다.

from tkinter import * 
from math import * 
root = Tk() 

label_1 = Label(root, text="Az osszes elem szama:") 
label_2 = Label(root, text="Az A halmaz elemeinek szama:") 
label_3 = Label(root, text="A B halmaz elemeinek szama:") 
label_4 = Label(root, text="A C halmaz elemeinek szama:") 
label_5 = Label(root, text="Az A es B halmaz metszetenek elemeinek szama:") 
label_6 = Label(root, text="Az A es C halmaz metszetenek elemeinek szama:") 
label_7 = Label(root, text="Az C es B halmaz metszetenek elemeinek szama:") 
label_8 = Label(root, text="Az A es B es C halmaz metszetenek elemeinek szama:") 

U = Entry(root) 
AH = Entry(root) 
BH = Entry(root) 
CH = Entry(root) 
AHmBH = Entry(root) 
AHmCH = Entry(root) 
CHmBH = Entry(root) 
AHmBHmCH = Entry(root) 

label_1.grid(row=0, sticky=E) 
label_2.grid(row=1, sticky=E) 
label_3.grid(row=2, sticky=E) 
label_4.grid(row=3, sticky=E) 
label_5.grid(row=4, sticky=E) 
label_6.grid(row=5, sticky=E) 
label_7.grid(row=6, sticky=E) 
label_8.grid(row=7, sticky=E) 

U.grid(row=0, column=1) 
AH.grid(row=1, column=1) 
BH.grid(row=2, column=1) 
CH.grid(row=3, column=1) 
AHmBH.grid(row=4, column=1) 
AHmCH.grid(row=5, column=1) 
CHmBH.grid(row=6, column=1) 
AHmBHmCH.grid(row=7, column=1) 

U = IntVar() 
AH = IntVar() 
BH = IntVar() 
CH = IntVar() 
AHmBH = IntVar() 
AHmCH = IntVar() 
CHmBH = IntVar() 
AHmBHmCH = IntVar() 

E = int(U.get()) - (int(AH.get()) + int(BH.get()) + int(CH.get())) + 
    (int(AHmBH.get()) + int(AHmCH.get()) + int(CHmBH.get())) - int(AHmBHmCH.get()) 

def calculate(event): 
    if E < 0: 
     print("0-nal nem lehet kisebb") 

     if (int(AHmBH.get()) - int(AHmBHmCH.get())) + int(AHmBHmCH.get()) + (int(AHmCH.get()) - int(AHmBHmCH.get())) <= int(AH.get()) and (int(AHmCH.get()) - int(AHmBHmCH.get())) + int(AHmBHmCH.get()) + (
      int(CHmBH.get()) - int(AHmBHmCH.get())) <= int(CH.get()) and (int(AHmBH.get()) - int(AHmBHmCH.get())) + int(AHmBHmCH.get()) + (int(AHmCH.get()) - int(AHmBHmCH.get())) != int(AH.get()) and int(E) >= 0: 
      print(int(E)) 
      print(" db elem nem tartozik A B vagy C halmazokba") 
    else: 
     print("A megadott adatok nem valosak") 

button_1 = Button(root, text="szamitas") 
button_1.bind("<Button-1>", calculate) 
button_1.grid(row=8, columnspan=2) 

root.mainloop() 
+0

들여 쓰기를 수정하십시오 – Goralight

+0

[this] (https://stackoverflow.com/help/mcve) 문서를 읽고 질문을 다시 작성하십시오. 당신을 도우려는 사람들에게 많은 노력을 기울였습니다. 우리가 당신을 도울 수 있도록 돕고, Minimal, Complete 및 Verifiable 예제 인 –

+0

을 작성하는 것은 매우 간단합니다. 전에 계산을 할 당시의 가치를 얻어야합니다. –

답변

4

Entry 위젯을 덮어 쓰고 있습니다.

마지막 코드는 다음과 같이한다

U = IntVar() 
AH = IntVar() 
BH = IntVar() 
CH = IntVar() 
AHmBH = IntVar() 
AHmCH = IntVar() 
CHmBH = IntVar() 
AHmBHmCH = IntVar() 

그리고 당신의 Calculate 기능 내에서 E 변수를 배치 :

다음 코드를 제거

from tkinter import * 
from math import * 
root = Tk() 

label_1 = Label(root, text="Az osszes elem szama:") 
label_2 = Label(root, text="Az A halmaz elemeinek szama:") 
label_3 = Label(root, text="A B halmaz elemeinek szama:") 
label_4 = Label(root, text="A C halmaz elemeinek szama:") 
label_5 = Label(root, text="Az A es B halmaz metszetenek elemeinek szama:") 
label_6 = Label(root, text="Az A es C halmaz metszetenek elemeinek szama:") 
label_7 = Label(root, text="Az C es B halmaz metszetenek elemeinek szama:") 
label_8 = Label(root, text="Az A es B es C halmaz metszetenek elemeinek szama:") 

U = Entry(root) 
AH = Entry(root) 
BH = Entry(root) 
CH = Entry(root) 
AHmBH = Entry(root) 
AHmCH = Entry(root) 
CHmBH = Entry(root) 
AHmBHmCH = Entry(root) 

label_1.grid(row=0, sticky=E) 
label_2.grid(row=1, sticky=E) 
label_3.grid(row=2, sticky=E) 
label_4.grid(row=3, sticky=E) 
label_5.grid(row=4, sticky=E) 
label_6.grid(row=5, sticky=E) 
label_7.grid(row=6, sticky=E) 
label_8.grid(row=7, sticky=E) 

U.grid(row=0, column=1) 
AH.grid(row=1, column=1) 
BH.grid(row=2, column=1) 
CH.grid(row=3, column=1) 
AHmBH.grid(row=4, column=1) 
AHmCH.grid(row=5, column=1) 
CHmBH.grid(row=6, column=1) 
AHmBHmCH.grid(row=7, column=1) 


def calculate(event): 
    E = int(U.get()) - (int(AH.get()) + int(BH.get()) + int(CH.get())) + (int(AHmBH.get()) + int(AHmCH.get()) + int(CHmBH.get())) - int(AHmBHmCH.get()) 
    # print E 
    if E < 0: 
     print("0-nal nem lehet kisebb") 

    if (int(AHmBH.get()) - int(AHmBHmCH.get())) + int(AHmBHmCH.get()) + (int(AHmCH.get()) - int(AHmBHmCH.get())) <= int(AH.get()) and (int(AHmCH.get()) - int(AHmBHmCH.get())) + int(AHmBHmCH.get()) + (int(CHmBH.get()) - int(AHmBHmCH.get())) <= int(CH.get()) and (int(AHmBH.get()) - int(AHmBHmCH.get())) + int(AHmBHmCH.get()) + (int(AHmCH.get()) - int(AHmBHmCH.get())) != int(AH.get()) and int(E) >= 0: 
     print(int(E)) 
     print(" db elem nem tartozik A B vagy C halmazokba") 
    else: 
     print("A megadott adatok nem valosak") 

button_1 = Button(root, text="szamitas") 
button_1.bind("<Button-1>", calculate) 
button_1.grid(row=8, columnspan=2) 

root.mainloop() 

내가 추가해야합니다 당신이 들여 쓰기가 여기 저기에 있습니다. 탭으로 만 또는 4 칸을 유지하십시오. 섞어서는 안되며, 1/2/3 공백의 들여 쓰기를 사용하지 마십시오.

그리고 나중에는 의미있는 변수 이름을 사용하십시오. E, U 등은별로 의미가 없으며 실제로 모호합니다. 이것을 피하십시오.

+1

너무 빨리 도와 주셔서 감사합니다! 프로그래밍에 익숙하지 않아 들여 쓰기가 어떻게 작동했는지 정말 몰랐습니다. 너무 감사드립니다! –

관련 문제