누군가가이 문제로 나를 도울 수 있기를 바랍니다.
from tkinter import *#This enables me to use the tkinter commands
window=Tk()#This declares the window
window.title("Binary-Denary converters")#This defines the name of the window
loop=1
def selection():
global submitbutton
global variable
global choice#This declares the variable so it can be used anywhere in the code
label1=Label(window,text="Submit 1 for D-B \nSubmit 2 for B-D ")#This tells the user what to input
label1.pack()
variable= StringVar(window)
variable.set("")
choice=OptionMenu(window, variable,"1 ", "2 ")
choice.pack()
submitbutton=Button(window, text="Submit",command=getinput)
submitbutton.pack()
def getinput():
global variable
global userinput
userinput=variable.get()#This takes the users input and assigns it to a variable
print(userinput)
if userinput =="1":
DToB()
else:
BToD()
def DToB():
display1=Label(window, text="D to B")
display1.pack()
submitbutton.destroy()
def BToD():
display2=Label(window, text="B to D ")
display2.pack()
submitbutton.destroy()
selection()
사용자는 드롭 다운 목록을 가지고 있고 DToB에 대해 1을 선택하고 BToD에 대해 2를 선택하면 프로그램은 사용자가 선택한 번호를 식별 할 수 있으며 userinput을 인쇄하여 확인했습니다. 나는 또한 확인 했고이 드롭 다운 목록에서 오는 str 값입니다 .userinput에 userinput을 추가하여 int 인 경우 2 대신 1 1을 제공하여 이것을 확인했습니다.
문제는 getinput () 함수의 if 문 "if userinput =="1 ""에 있습니다. 이는 userinput이 = 1 일 때도 명령문의 else 부분에있는 것과 함께 진행됩니다.
이전에 매우 유사한 코드에서 이와 같은 if 문을 사용했기 때문에 내가 뭘 잘못했는지 이해할 수 없습니다.
문제는 다음과 같습니다.
choice = OptionMenu(window, variable, "1 ", "2 ")
사용자 1을 선택하면,의 값은 StringVar
실제로로 설정 "1 "
하지 "1"
. 옵션 메뉴의 값을 변경하거나로 변경 if userinput == "1"
하면 if userinput = "1 "
코드가 예상대로 작동합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다