1분 퀴즈

    2 다음은 sayhello 리스트의 값을 화면에 보여 주는 프로그램입니다. [-->] 버튼을 누를 때마다 화면에 현재 보이는 값의 오른쪽 요소 값이 화면에 표시됩니다. 코드를 보고 틀린 설명을 고르세요.

    from tkinter import * 
    
    sayhello = ['안녕하세요', 'hello', '니하오', '봉쥬르'] 
    idx = 0    # sayhello 리스트의 인덱스
    
    # ----- 프로그램 기능 부분 ----- 
    def plus(): 
        (가) 
        idx = (idx + 1) % 4 
        (나) 
    
    # ----- 프로그램 화면 부분 ----- 
    root = Tk() 
    root.geometry('200x100') 
    
    hello_txt = StringVar() 
    hello_lbl = Label(root, textvariable=hello_txt, width=20) 
    hello_lbl.grid(row=0, column=0) 
    
    btn_plus = Button(root, text='-->', command=plus, width=5) 
    btn_plus.grid(row=0, column=1) 
    
    root.mainloop()
    실행결과

     

    idx는 전역변수다.

    plus() 함수에서 idx를 변경하려면 (가)에는 global idx가 들어가야 한다.

    ③ (나)에 들어갈 코드는 hello_txt.get(sayhello[idx])다.

    hello_txt는 동적변수다.

    ⑤ 버튼을 클릭할 때마다 plus()가 실행된다.

    정답 및 해설

    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.