<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Tkinter 中的對話框 > 原文: [http://zetcode.com/tkinter/dialogs/](http://zetcode.com/tkinter/dialogs/) 在 Tkinter 教程的這一部分中,我們將使用對話框。 對話框窗口或對話框是大多數現代 GUI 應用必不可少的部分。 對話被定義為兩個或更多人之間的對話。 在計算機應用中,對話框是一個窗口,用于與應用“對話”。 對話框用于輸入數據,修改數據,更改應用設置等。對話框是用戶與計算機程序之間進行通信的重要手段。 ## Tkinter 消息框 消息框是方便的對話框,可向應用的用戶提供消息。 該消息由文本和圖像數據組成。 Tkinter 中的消息框位于`tkMessageBox`模塊中。 `messagebox.py` ```py #!/usr/bin/env python3 """ ZetCode Tkinter tutorial In this program, we show various message boxes. Author: Jan Bodnar Last modified: April 2019 Website: www.zetcode.com """ from tkinter import Tk, BOTH from tkinter.ttk import Frame, Button from tkinter import messagebox as mbox class Example(Frame): def __init__(self): super().__init__() self.initUI() def initUI(self): self.master.title("Message boxes") self.pack() error = Button(self, text="Error", command=self.onError) 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) def onError(self): mbox.showerror("Error", "Could not open file") def onWarn(self): mbox.showwarning("Warning", "Deprecated function call") def onQuest(self): mbox.askquestion("Question", "Are you sure to quit?") def onInfo(self): mbox.showinfo("Information", "Download completed") def main(): root = Tk() ex = Example() root.geometry("300x150+300+300") root.mainloop() if __name__ == '__main__': main() ``` 我們使用網格管理器來設置四個按鈕的網格。 每個按鈕顯示一個不同的消息框。 ```py from tkinter import messagebox as mbox ``` 我們導入`messagebox`,它具有顯示對話框的功能。 ```py error = Button(self, text="Error", command=self.onError) ``` 我們創建一個錯誤按鈕,該按鈕調用`onError()`方法。 在方法內部,我們顯示錯誤消息對話框。 ```py def onError(self): mbox.showerror("Error", "Could not open file") ``` 如果按下錯誤按鈕,則會顯示錯誤對話框。 我們使用`showerror()`函數在屏幕上顯示對話框。 此方法的第一個參數是消息框的標題,第二個參數是實際消息。 ![Error message dialog](https://img.kancloud.cn/e9/69/e9697fbd83339bdbb14a473f7058ce40_223x118.jpg) 圖:錯誤消息 dialog ## Tkinter 顏色選擇器 顏色選擇器是用于選擇顏色的對話框。 `color_chooser.py` ```py #!/usr/bin/env python3 """ ZetCode Tkinter tutorial In this script, we use colorchooser dialog to change the background of a frame. Author: Jan Bodnar Last modified: April 2019 Website: www.zetcode.com """ from tkinter import Tk, Frame, Button, BOTH, SUNKEN from tkinter import colorchooser class Example(Frame): def __init__(self): super().__init__() self.initUI() def initUI(self): self.master.title("Color chooser") self.pack(fill=BOTH, expand=1) self.btn = Button(self, text="Choose Color", command=self.onChoose) self.btn.place(x=30, y=30) self.frame = Frame(self, border=1, relief=SUNKEN, width=100, height=100) self.frame.place(x=160, y=30) def onChoose(self): (rgb, hx) = colorchooser.askcolor() self.frame.config(bg=hx) def main(): root = Tk() ex = Example() root.geometry("300x150+300+300") root.mainloop() if __name__ == '__main__': main() ``` 我們有一個按鈕和一個框架。 單擊按鈕,我們顯示一個顏色選擇器對話框。 我們將通過從對話框中選擇一種顏色來更改框架的背景顏色。 ```py (rgb, hx) = colorchooser.askcolor() self.frame.config(bg=hx) ``` `askcolor()`函數顯示對話框。 如果單擊“確定”,則返回一個元組。 它是 RGB 和十六進制格式的顏色值。 在第二行中,我們使用返回的顏色值更改框架的背景顏色。 ![Color chooser](https://img.kancloud.cn/28/ed/28edb0af9c498a973691dd9451229f4d_418x221.jpg) 圖:顏色選擇器 ## Tkinter 文件對話框 `tkFileDialog`對話框允許用戶從文件系統中選擇文件。 `file_dialog.py` ```py #!/usr/bin/env python3 """ ZetCode Tkinter tutorial In this program, we use the tkFileDialog to select a file from a filesystem. Author: Jan Bodnar Last modified: April 2019 Website: www.zetcode.com """ from tkinter import Frame, Tk, BOTH, Text, Menu, END from tkinter import filedialog class Example(Frame): def __init__(self): super().__init__() self.initUI() def initUI(self): self.master.title("File dialog") self.pack(fill=BOTH, expand=1) menubar = Menu(self.master) self.master.config(menu=menubar) fileMenu = Menu(menubar) fileMenu.add_command(label="Open", command=self.onOpen) menubar.add_cascade(label="File", menu=fileMenu) self.txt = Text(self) self.txt.pack(fill=BOTH, expand=1) def onOpen(self): ftypes = [('Python files', '*.py'), ('All files', '*')] dlg = filedialog.Open(self, filetypes = ftypes) fl = dlg.show() if fl != '': text = self.readFile(fl) self.txt.insert(END, text) def readFile(self, filename): with open(filename, "r") as f: text = f.read() return text def main(): root = Tk() ex = Example() root.geometry("300x250+300+300") root.mainloop() if __name__ == '__main__': main() ``` 在我們的代碼示例中,我們使用`tkFileDialog`對話框選擇一個文件,并在`Text`小部件中顯示其內容。 ```py self.txt = Text(self) ``` 這是`Text`小部件,我們將在其中顯示所選文件的內容。 ```py ftypes = [('Python files', '*.py'), ('All files', '*')] ``` 這些是文件過濾器。 第一個僅顯示 Python 文件,另一個顯示所有文件。 ```py dlg = filedialog.Open(self, filetypes = ftypes) fl = dlg.show() ``` 該對話框已創建并顯示在屏幕上。 我們得到返回值,它是所選文件的名稱。 ```py text = self.readFile(fl) ``` 我們讀取了文件的內容。 ```py self.txt.insert(END, text) ``` 文本被插入到`Text`小部件中。 ![File dialog](https://img.kancloud.cn/8f/a1/8fa19dba8e58db50b72e1d446a294a4d_418x278.jpg) 圖:文件對話框 在 Tkinter 教程的這一部分中,我們使用了對話框窗口。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看