Tkinter - 如何获取整数条目值?
Tkinter - How to get integer Entry values?
我试过根据其他人的代码来做同样的问题,但我做不到。
这里的问题是代码在执行 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()
您正在覆盖 Entry
小部件。
删除以下代码:
U = IntVar()
AH = IntVar()
BH = IntVar()
CH = IntVar()
AHmBH = IntVar()
AHmCH = IntVar()
CHmBH = IntVar()
AHmBHmCH = IntVar()
并将您的 E
变量放入您的 Calculate
函数中
最终代码应如下所示:
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
等,意义不大,很含糊。尽量避免这种情况。
我试过根据其他人的代码来做同样的问题,但我做不到。
这里的问题是代码在执行 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()
您正在覆盖 Entry
小部件。
删除以下代码:
U = IntVar()
AH = IntVar()
BH = IntVar()
CH = IntVar()
AHmBH = IntVar()
AHmCH = IntVar()
CHmBH = IntVar()
AHmBHmCH = IntVar()
并将您的 E
变量放入您的 Calculate
函数中
最终代码应如下所示:
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
等,意义不大,很含糊。尽量避免这种情况。