Hướng dẫn messagebox askyesno python - hộp thư askyesno python

Trong hướng dẫn ngắn này, bạn sẽ thấy cách tạo một hộp thông báo có/không trong Python bằng Tkinter. Điêu nay bao gôm:Yes/No message box in Python using tkinter. This includes:

  • Một GUI đơn giản có một nút, được gọi là nút ‘Ứng dụng thoát‘Exit Application‘ button
  • Khi bạn nhấn vào nút đó, bạn sẽ có một lựa chọn giữa:
    • Có - để đóng GUI – to close the GUI
    • Không - Để trở lại màn hình chính – to return back to the main display

Mã được sử dụng để tạo hộp thông báo có/không trong Python

Dưới đây là mã đầy đủ để tạo hộp thông báo Có/Không trong Python bằng Tkinter.

Trong phần tiếp theo, bạn sẽ thấy một lời giải thích về từng thành phần được sử dụng trong mã.

import tkinter as tk
from tkinter import messagebox

root = tk.Tk()

canvas1 = tk.Canvas(root, width=300, height=300)
canvas1.pack()


def exit_application():
    msg_box = tk.messagebox.askquestion('Exit Application', 'Are you sure you want to exit the application?',
                                        icon='warning')
    if msg_box == 'yes':
        root.destroy()
    else:
        tk.messagebox.showinfo('Return', 'You will now return to the application screen')


button1 = tk.Button(root, text='Exit Application', command=exit_application, bg='brown', fg='white')
canvas1.create_window(150, 150, window=button1)

root.mainloop()

Nền về mã

Bây giờ, hãy xem xét các thành phần được sử dụng trong mã.

(1) Đầu tiên, nhập gói tkinter & nbsp; Gói này được sử dụng để tạo giao diện người dùng đồ họa (GUI) trong Python:

import tkinter as tk
from tkinter import messagebox

(2) Sau đó, tạo khung vẽ, sẽ trở thành màn hình GUI của bạn. Sau đó, bạn có thể đặt các mục, chẳng hạn như các nút, trên vải.

Lưu ý rằng bạn có thể thay đổi chiều rộng và chiều cao của màn hình bằng cách sửa đổi các giá trị số bên dưới:

canvas1 = tk.Canvas(root, width=300, height=300)
canvas1.pack()

(3) Bây giờ, tạo một chức năng được gọi bằng nút ứng dụng thoát (Nút AKA1).

Khi bạn nhấn vào nút, một hộp thông báo sẽ xuất hiện trên màn hình của bạn. Bạn có thể chỉnh sửa văn bản trong hộp thông báo:

msg_box = tk.messageBox.askQuestion (gõ đây là tiêu đề cho hộp thông báo của bạn, ‘Nhập ở đây nội dung sẽ được hiển thị trong hộp thông báo)

Sau đó, bạn sẽ cần sử dụng chức năng IF:

  • Nếu MSG_BOX == Có thì GUI sẽ bị đóngthen the GUI will be closed
  • Khác: Một hộp thông báo khác sẽ xuất hiện trên màn hình của bạn để cho biết rằng bây giờ bạn sẽ quay lại màn hình ứng dụng another message box would appear on your screen to indicate that you’ll now return back to the application screen
def exit_application():
    msg_box = tk.messagebox.askquestion('Exit Application', 'Are you sure you want to exit the application?',
                                        icon='warning')
    if msg_box == 'yes':
        root.destroy()
    else:
        tk.messagebox.showinfo('Return', 'You will now return to the application screen')

.

Nút nên được đặt trên GUI. Bạn có thể kiểm soát vị trí của nút bằng cách thay đổi các giá trị số bên dưới:

button1 = tk.Button(root, text='Exit Application', command=exit_application, bg='brown', fg='white')
canvas1.create_window(150, 150, window=button1)

Chạy mã Python

Chạy mã Python và bạn sẽ thấy một GUI đơn giản có một nút duy nhất để thoát khỏi ứng dụng.

Nhấn vào nút ‘Thoát ứng dụng và bạn sẽ nhận được một hộp thông báo, nơi bạn sẽ có hai tùy chọn để lựa chọn. Nếu bạn nhấp vào ‘Có‘, bạn sẽ thoát khỏi ứng dụng.Yes‘, you’ll exit the application.

Bạn có chắc là bạn muốn thoát khỏi ứng dụng? Có & nbsp; & nbsp; & nbsp; Không
Yes      No

Tuy nhiên, nếu bạn nhấp vào ‘Không‘, bạn sẽ nhận được hộp thông báo thứ hai này:No‘, you’ll get this second message box:

Bây giờ bạn sẽ trở lại màn hình ứng dụng

Khi bạn nhấp vào ‘OK, bạn sẽ quay lại màn hình ứng dụng.

Bạn có thể kiểm tra hướng dẫn sau đây để biết GUI toàn diện hơn mà bạn có thể tạo bằng Tkinter hoặc truy cập tài liệu Tkinter.



Các Khăn HọC Qua Video:
Lập trình c java c# sql máy chủ php html5-css3-javaScript

«Prev: Python: BÀi 4. Menu

»Tiếp theo: Python: BÀi 6. Đồ HọA

Trong bài nào chún ta sẽ học cách hiển thị & nbsp;

Hộp tin nhắn
from tkinter import Tk, BOTH
import tkinter.messagebox as mbox

Đây Là Kiểu Hôp thoại Dùng Để Hiển Th
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initUI()

Từ khung nhập của tkinter.ttk, nút từ nhập tker tk, twoomport tkinter.messageBox dưới dạng mbox
        self.parent.title("Message Boxes")
        self.pack()

Lớp ví dụ (khung): & nbsp; & nbsp; def __init __ (tự, cha mẹ): & nbsp; & nbsp; & nbsp; & nbsp; Khung .__ init __ (tự, cha mẹ) & nbsp; & nbsp; & nbsp; & nbsp; self.parent = cha mẹ & nbsp; & nbsp; & nbsp; & nbsp; self.initui ()
        error.grid(padx=5, pady=5)
        warning = Button(self, text="Warning", command=self.onWarn)
        warning.grid(row=1, column=0)
        question = Button(self, text="Question", command=self.onQuest)
        question.grid(row=0, column=1)
        inform = Button(self, text="Information", command=self.onInfo)
        inform.grid(row=1, column=1)

& nbsp; & nbsp; & nbsp; & nbsp; def initui (tự): & nbsp; & nbsp; & nbsp; & nbsp; self.parent.title ("hộp tin nhắn") & nbsp; & nbsp; & nbsp; & nbsp; self.pack ()
        mbox.showerror("Error", "Could not open file")

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; error = nút (self, text = "error", lệnh = self.onerror) & nbsp; & nbsp; & nbsp; & nbsp; Lỗi.grid (padx = 5, pady = 5) & nbsp; & nbsp; & nbsp; & nbsp; cảnh báo = nút (tự, văn bản = "cảnh báo", lệnh = self.onwarn) & nbsp; & nbsp; & nbsp; & nbsp; cảnh báo.grid (hàng = 1, cột = 0) & nbsp; & nbsp; & nbsp; & nbsp; câu hỏi = nút (tự, văn bản = "câu hỏi", lệnh = self.onquest) & nbsp; & nbsp; & nbsp; & nbsp; câu hỏi.grid (hàng = 0, cột = 1) & nbsp; & nbsp; & nbsp; & nbsp; thông tin = nút (self, text = "thông tin", lệnh = self.oninfo) & nbsp; & nbsp; & nbsp; & nbsp; inform.grid (hàng = 1, cột = 1)
        mbox.showwarning("Warning", "Deprecated function call")

& nbsp; & nbsp; & nbsp; & nbsp; def onerror (tự): & nbsp; & nbsp; & nbsp; & nbsp; mbox.showerror ("lỗi", "không thể mở tệp")
        mbox.askquestion("Question", "Are you sure to quit?")

& nbsp; & nbsp; & nbsp; & nbsp; def onwarn (self): & nbsp; & nbsp; & nbsp; & nbsp; mbox.showwarning ("Cảnh báo", "Gọi chức năng không dùng nữa")
        mbox.showinfo("Information", "Download completed")

& nbsp; & nbsp; & nbsp; & nbsp; def onquest (tự): & nbsp; & nbsp; & nbsp; & nbsp; mbox.askquestion ("câu hỏi", "bạn có chắc chắn không?")
ex = Example(root)
root.geometry("300x150+300+300")
root.mainloop()

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; def oninfo (tự): & nbsp; & nbsp; & nbsp; & nbsp; mbox.showinfo ("thông tin", "hoàn thành tải xuống")

& nbsp; & nbsp; root = tk () ex = example (root) root.geometry ("300x150+300+300") root.mainloop ()

Trong ví dụ trênn chún ta hiển thị 4 nút, nút mỗi sẽ hiển thị một kiểu hộp thông báo khát nhau.

Nhập Tkinter.MessageBox dưới dạng Mbox

Để sử dụng Đ-c hộp thông báo thì bạn Nhập mô -đun & nbsp; ________ 5 & nbsp; (hoặc & nbsp;

error = nút (self, text = "error", lệnh = self.onerror)
        mbox.showerror("Error", "Could not open file")

Ở dengng trênn chún tạo nút & nbsp; lỗi, & nbsp; Khi

  • askokcancel(title, message, option)
  • import tkinter as tk
    from tkinter import messagebox
    
    0
  • import tkinter as tk
    from tkinter import messagebox
    
    1
  • import tkinter as tk
    from tkinter import messagebox
    
    2
  • import tkinter as tk
    from tkinter import messagebox
    
    3
  • import tkinter as tk
    from tkinter import messagebox
    
    4

def onerror (tự): & nbsp; & nbsp; & nbsp; & nbsp; mbox.showerror ("lỗi", "không thể mở tệp")

Để hiển thị

Hướng dẫn messagebox askyesno python - hộp thư askyesno python

Hộp thoại chọn màu (người chọn màu)

Các Hệ Điều HànH Hay Có Sẵn Hộp Thoại Chọn Màu Cho Chún Ta Sử Dụng. Để hiển thị hộp thoại nÀy chún ta mô -đun & nbsp; ________ 23 (hoặc & nbsp; ________ 24n

từ tkinter nhập tk, khung, nút, cả hai, sunkenfrom tkinter.colorchooser nhập AskColor
from tkinter.colorchooser import askcolor

Lớp ví dụ (khung): & nbsp; & nbsp; def __init __ (tự, cha mẹ): & nbsp; & nbsp; & nbsp; & nbsp; Khung .__ init __ (tự, cha mẹ)
    def __init__(self, parent):
        Frame.__init__(self, parent)

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; self.parent = cha mẹ & nbsp; & nbsp; & nbsp; & nbsp; self.initui ()
        self.initUI()

& nbsp; & nbsp; & nbsp; & nbsp; def initui (tự): & nbsp; & nbsp; & nbsp; & nbsp; self.parent.title ("trình chọn màu") & nbsp; & nbsp; & nbsp; & nbsp; self.pack (fill = cả hai, mở rộng = 1)
        self.parent.title("Color chooser")
        self.pack(fill=BOTH, expand=1)

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; self.btn = nút (self, text = "chọn màu", lệnh = self.onchoose) & nbsp; & nbsp; & nbsp; & nbsp; self.btn.place (x = 30, y = 30)
        self.btn.place(x=30, y=30)

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; self.frame = frame (self, border = 1, beauple = sunken, width = 100, height = 100) & nbsp; & nbsp; & nbsp; & nbsp; self.frame.place (x = 160, y = 30)
        self.frame.place(x=160, y=30)

& nbsp; & nbsp; & nbsp; & nbsp; def onchoose (tự): & nbsp; & nbsp; & nbsp; & nbsp; (rgb, hx) = AskColor () & nbsp; & nbsp; & nbsp; & nbsp; self.frame.config (bg = hx)
        (rgb, hx) = askcolor()
        self.frame.config(bg=hx)

root = tk () ex = example (root) root.geometry ("300x150+300+300") root.mainloop ()
ex = Example(root)
root.geometry("300x150+300+300")
root.mainloop()

Trong ví dụ trên chún ta hiển thị một nút và một frame lênn cửa sổ chính. Nút Sẽ Mởt Hộp Thoại Chọn Màu, Chọn Màu NÀO THÌ & NBSP;

.
self.frame.config(bg=hx)

Hiển ển thị HÀM & nbsp; ________ 26bênn trong phương thức ________ 27để biết giá trị hexa nào. Chún ta dùng giá tị hexa để ôn

Hướng dẫn messagebox askyesno python - hộp thư askyesno python

Hộp Thoại Chọn Tệp (Hộp thoại Tệp)

Trong ví dụ dưới đNy chún ta sẽ dùng hào & nbsp; ________ 28 & nbsp; của mô -đun & nbsp; ________ 29 & nbsp;

Từ khung nhập tkinter, tk, cả hai, văn bản, menu, endfrom tkinter.filealialb nhập mở
from tkinter.filedialog import Open

Lớp ví dụ (khung): & nbsp; & nbsp; def __init __ (tự, cha mẹ): & nbsp; & nbsp; & nbsp; & nbsp; Khung .__ init __ (tự, cha mẹ) & nbsp; & nbsp; & nbsp; & nbsp; self.parent = cha mẹ & nbsp; & nbsp; & nbsp; & nbsp; self.initui ()
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initUI()

& nbsp; & nbsp; & nbsp; & nbsp; def initui (tự): & nbsp; & nbsp; & nbsp; & nbsp; self.parent.title ("hộp thoại tệp") & nbsp; & nbsp; & nbsp; & nbsp; self.pack (fill = cả hai, mở rộng = 1)
        self.parent.title("File dialog")
        self.pack(fill=BOTH, expand=1)

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; menubar = menu (self.parent) & nbsp; & nbsp; & nbsp; & nbsp; self.parent.config (menu = menubar)
        self.parent.config(menu=menubar)

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; filemenu = menu (menubar) & nbsp; & nbsp; & nbsp; & nbsp; filemenu.add_command (nhãn = "mở", lệnh = self.onopen) & nbsp; & nbsp; & nbsp; & nbsp; menubar.add_cascade (nhãn = "file", menu = filemenu)
        fileMenu.add_command(label="Open", command=self.onOpen)
        menubar.add_cascade(label="File", menu=fileMenu)

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; self.txt = text (self) & nbsp; & nbsp; & nbsp; & nbsp; self.txt.pack (fill = cả hai, mở rộng = 1)
        self.txt.pack(fill=BOTH, expand=1)

& nbsp; & nbsp; & nbsp; & nbsp; def onopen (tự): & nbsp; & nbsp; & nbsp; & nbsp; ftypes = [('tệp python', '*.py'), ('tất cả các tệp', '*')] & nbsp; & nbsp; & nbsp; & nbsp; dlg = open (self, filetypes = ftypes) & nbsp; & nbsp; & nbsp; & nbsp; fl = dlg.show () & nbsp; & nbsp; & nbsp; & nbsp; Nếu fl! = '': & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Text = self.ReadFile (fl) & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; self.txt.insert (kết thúc, văn bản)
        ftypes = [('Python files', '*.py'), ('All files', '*')]
        dlg = Open(self, filetypes = ftypes)
        fl = dlg.show()
        if fl != '':
            text = self.readFile(fl)
            self.txt.insert(END, text)

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; def ReadFile (tự, tên tệp): & nbsp; & nbsp; & nbsp; & nbsp; f = open (tên tệp, "r") & nbsp; & nbsp; & nbsp; & nbsp; Text = f.Read () & nbsp; & nbsp; & nbsp; & nbsp; trả lại văn bản
        f = open(filename, "r")
        text = f.read()
        return text

& nbsp; & nbsp; root = tk () ex = example (root) root.geometry ("300x250+300+300") root.mainloop ()
ex = Example(root)
root.geometry("300x250+300+300")
root.mainloop()

Chúng ta sẽ tạo Một Menu Cho Phép Mở Hộp thoại File Và Đan nội Dung Của Tệp Đ-C ChọN VÀO LớP & NBSP; ________ 30.

ftypes = [('tệp python', '*.py'), ('tất cả các tệp', '*')]]]

Dngng trên là & nbsp; lise lưu các họ tệp dạng kháta nhau để lọc.

dlg = open (self, filetypes = ftypes) fl = dlg.show ()
fl = dlg.show()

Để Hiển Thị Hộp thoại Tệp Thì Chún Ta Gọi HÀM & NBSP; ________ 31. & NBSP;

Hướng dẫn messagebox askyesno python - hộp thư askyesno python

Hướng dẫn messagebox askyesno python - hộp thư askyesno python

Các Khăn HọC Qua Video:
Lập trình c java c# sql máy chủ php html5-css3-javaScript
«Prev: Python: BÀi 4. Menu Prev: Python: Bài 4. Menu
»Tiếp theo: Python: BÀi 6. Đồ HọA Next: Python: Bài 6. Đồ họa