from tkinter import *
from ProjectHeader import *
def sel1():
return 1
def sel2():
return 2
def sel3():
return 3
def sel4():
return 4
def sel():
selection = "THe answer is: " + str(sel2() + sel3())
label.config(text = selection)
top = Tk()
var = IntVar()
CheckVar1 = sel1()
CheckVar2 = sel2()
CheckVar3 = sel3()
CheckVar4 = sel4()
C1 = Checkbutton(top, text = "Option1", variable = CheckVar1)
C2 = Checkbutton(top, text = "Option2", variable = CheckVar2)
C3 = Checkbutton(top, text = "Option3", variable = CheckVar3)
C4 = Checkbutton(top, text = "Option4", variable = CheckVar4)
B = Button(top, text ="ADD", command=sel)
B.pack()
C1.pack()
C2.pack()
C3.pack()
C4.pack()
label = Label(top)
label.pack()
top.mainloop()
제목이 말했듯이, 서로 다른 두 개의 확인란에서 두 개의 숫자를 추가하는 GUI를 만드는 방법은 무엇입니까?
예를 들어, 옵션 2와 옵션 3을 모두 선택하면 프로그램은 sel2 () 및 sel3 ()의 값을 가져 와서 추가합니다.
몇 가지 방법으로 시도했지만 확인란을 true로 설정하는 방법을 이해하지 못합니다 / 확인란을 선택할 때 선택하면 상자가 선택되지 않아도 코드가 답을 보여줍니다.
감사
다음은 내가 올바르게 이해하고 있다면 질문에 답할 프로그램의 단순화 된 버전입니다.
from Tkinter import *
gui = Tk()
#create variables to store check state
checked1 = IntVar()
checked2 = IntVar()
#create values for the two boxes
cb1 = 5
cb2 = 10
#create a callback for our button
def callback():
print(checked1.get()*cb1+checked2.get()*cb2)
c1 = Checkbutton(gui, text='b1', variable=checked1)
c2 = Checkbutton(gui, text='b2', variable=checked2)
b1 = Button(gui, text="ADD", command=callback)
c1.pack()
c2.pack()
b1.pack()
gui.mainloop()
GUI를 클래스로 재구성하는 것이 도움이 될 프로그램의 복잡성 수준에 도달하고 있습니다. 이를 수행하는 방법에 대한 예제가 필요하면 Tkinter 문서를 읽으십시오 . 다음은 GUI를 사용자 정의 클래스로 사용한 예입니다.
from Tkinter import *
class Gui(object):
def __init__(self, parent):
self.top = parent
self.checked1 = IntVar()
self.checked2 = IntVar()
self.c1_value = 1
self.c2_value = 2
self.c1 = Checkbutton(self.top, text='b1', variable=self.checked1)
self.c2 = Checkbutton(self.top, text='b2', variable=self.checked2)
self.b1 = Button(self.top, text="ADD", command=self.callback)
self.l1 = Label(self.top)
self.c1.pack()
self.c2.pack()
self.b1.pack()
self.l1.pack()
def callback(self):
value = self.c1_value*self.checked1.get() + self.c2_value*self.checked2.get()
self.l1.config(text=str(value))
root = Tk()
my_window = Gui(root)
root.mainloop()
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다