Tkinter askyesno

2024. 3. 18. 21:52GUI/tkinter

요약 : 이 튜토리얼에서는 Tkinter 함수를 사용하여 사용자 확인을 요청하는 대화 상자를 표시하는 askyesno() 함수를 배웁니다.

Tkinter askyesno() 함수 소개

때로는 사용자에게 확인을 요청해야 하는 경우가 있습니다. 예를 들어, 사용자가 종료 버튼을 클릭하면 응용 프로그램을 정말로 닫을 것인지 묻고 싶습니다. 아니면 실수로 그렇게 했을 수도 있습니다.

 

 

사용자 확인을 요청하는 대화 상자를 표시하려면 askyesno()기능을 사용합니다.

대화 상자에는 제목, 메시지, 두 개의 버튼(예 및 아니요)이 있습니다.

버튼을 클릭하면 yes함수가 True를 반환합니다. 그러나 no 버튼을 클릭하면 False가 반환됩니다.

다음은 askyesno() 함수의 구문을 보여줍니다.

answer = askyesno(title, message, **options)

answer은 True 또는 False 부울 값입니다.

Tkinter에는 askquestion()이라는 또 다른 함수도 있는데, 이는 'yes' 또는 'no' 값을 가진 문자열을 반환한다는 점을 제외하면 askyesno() 함수와 유사합니다.

answer = askquestion(title, message, **options)

Tkinter askyesno() 함수 예

다음 프로그램은 Tkinter askyesno()함수를 사용하는 방법을 보여줍니다.

 

 

Quit 버튼을 클릭하면 확인 대화상자가 표시됩니다.`

 

 

yes 버튼을 클릭하면 애플리케이션이 종료됩니다. 그 외에는 그대로 유지됩니다.

In [2]:
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import askyesno

# create the root window
root = tk.Tk()
root.title('Tkinter Yes/No Dialog')
root.geometry('300x150')

# click event handler
def confirm():
    answer = askyesno(title='confirmation',
                    message='Are you sure that you want to quit?')
    if answer:
        root.destroy()


ttk.Button(
    root,
    text='Ask Yes/No',
    command=confirm).pack(expand=True)


# start the app
root.mainloop()

다음은 동일한 프로그램이지만 객체 지향 프로그래밍 접근 방식을 사용합니다.

In [3]:
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import askyesno, askquestion


class App(tk.Tk):
    def __init__(self):
        super().__init__()

        self.title('Tkinter Yes/No Dialog')
        self.geometry('300x150')

        # Quit button
        quit_button = ttk.Button(self, text='Quit', command=self.confirm)
        quit_button.pack(expand=True)

    def confirm(self):
        answer = askyesno(title='Confirmation',
                          message='Are you sure that you want to quit?')
        if answer:
            self.destroy()


if __name__ == "__main__":
    app = App()
    app.mainloop()

요약

  • askyesno()사용자 확인을 요청하는 대화 상자를 표시하려면 Tkinter 함수를 사용하십시오 .
  • yes 버튼을 클릭하면, askyesno() 함수가 True를 반환하고, 그렇지 않으면 False가 반환 됩니다.
  • askquestion() 함수는 대신 'yes' 또는 'no' 값이 포함된 문자열을 반환합니다.

'GUI > tkinter' 카테고리의 다른 글

Tkinter askretrycancel  (0) 2024.03.20
Tkinter askokcancel  (0) 2024.03.19
Tkinter messagebox  (0) 2024.03.17
Switching between Frames Using the Frame tkraise() Method  (1) 2024.03.16
Developing a Full Tkinter Object-Oriented Application  (0) 2024.03.15