Tkinter askokcancel

2024. 3. 19. 19:18GUI/tkinter

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

Tkinter Askokcancel() 함수 소개

이 askokcancel() 함수는 OK 및 Cancel라는 두 개의 버튼이 있는 확인 대화 상자를 표시합니다.

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

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

Tkinter Askokcancel() 예

다음 프로그램은 모두 삭제 버튼을 보여줍니다. 버튼을 클릭하면 프로그램은 OK 및 Cancel 두 개의 버튼이 있는 확인 대화 상자를 표시합니다.

 

OK 버튼을 클릭하면, 프로그램은 모든 데이터가 성공적으로 삭제되었음을 나타내는 메시지 상자를 표시합니다.

 

 

프로그램:

In [ ]:
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import askokcancel, showinfo, WARNING

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

# click event handler


def confirm():
    answer = askokcancel(
        title='Confirmation',
        message='Deleting will delete all the data.',
        icon=WARNING)

    if answer:
        showinfo(
            title='Deletion Status',
            message='The data is deleted successfully')


ttk.Button(
    root,
    text='Delete All',
    command=confirm).pack(expand=True)


# start the app
root.mainloop()

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

In [ ]:
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import askokcancel, showinfo, WARNING


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

        self.title('Tkinter Ok/Cancel Dialog')
        self.geometry('300x150')

        delete_button = ttk.Button(
            self,
            text='Delete All',
            command=self.confirm)

        delete_button.pack(expand=True)

    def confirm(self):
        answer = askokcancel(
            title='Confirmation',
            message='Deleting will delete all the data.',
            icon=WARNING)

        if answer:
            showinfo(
                title='Deletion Status',
                message='The data is deleted successfully')


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

요약

  • 두 개의 버튼 OK 과 Cancel이 있는 확인 대화 상자를 표시하려면 Tkinter askokcancel() 함수를 사용하십시오.
  • OK 버튼을 클릭하면, askokcancel() 함수는 True를 반환하고, Cancel 버튼을 클릭하면, 함수가 False를 반환합니다.
 

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

Tkinter Color Chooser  (0) 2024.03.21
Tkinter askretrycancel  (0) 2024.03.20
Tkinter askyesno  (0) 2024.03.18
Tkinter messagebox  (0) 2024.03.17
Switching between Frames Using the Frame tkraise() Method  (1) 2024.03.16