原文: http://zetcode.com/tkinter/dialogs/

在 Tkinter 教程的这一部分中,我们将使用对话框。

对话框窗口或对话框是大多数现代 GUI 应用必不可少的部分。 对话被定义为两个或更多人之间的对话。 在计算机应用中,对话框是一个窗口,用于与应用“对话”。 对话框用于输入数据,修改数据,更改应用设置等。对话框是用户与计算机程序之间进行通信的重要手段。

Tkinter 消息框

消息框是方便的对话框,可向应用的用户提供消息。 该消息由文本和图像数据组成。 Tkinter 中的消息框位于tkMessageBox模块中。

messagebox.py

  1. #!/usr/bin/env python3
  2. """
  3. ZetCode Tkinter tutorial
  4. In this program, we show various
  5. message boxes.
  6. Author: Jan Bodnar
  7. Last modified: April 2019
  8. Website: www.zetcode.com
  9. """
  10. from tkinter import Tk, BOTH
  11. from tkinter.ttk import Frame, Button
  12. from tkinter import messagebox as mbox
  13. class Example(Frame):
  14. def __init__(self):
  15. super().__init__()
  16. self.initUI()
  17. def initUI(self):
  18. self.master.title("Message boxes")
  19. self.pack()
  20. error = Button(self, text="Error", command=self.onError)
  21. error.grid(padx=5, pady=5)
  22. warning = Button(self, text="Warning", command=self.onWarn)
  23. warning.grid(row=1, column=0)
  24. question = Button(self, text="Question", command=self.onQuest)
  25. question.grid(row=0, column=1)
  26. inform = Button(self, text="Information", command=self.onInfo)
  27. inform.grid(row=1, column=1)
  28. def onError(self):
  29. mbox.showerror("Error", "Could not open file")
  30. def onWarn(self):
  31. mbox.showwarning("Warning", "Deprecated function call")
  32. def onQuest(self):
  33. mbox.askquestion("Question", "Are you sure to quit?")
  34. def onInfo(self):
  35. mbox.showinfo("Information", "Download completed")
  36. def main():
  37. root = Tk()
  38. ex = Example()
  39. root.geometry("300x150+300+300")
  40. root.mainloop()
  41. if __name__ == '__main__':
  42. main()

我们使用网格管理器来设置四个按钮的网格。 每个按钮显示一个不同的消息框。

  1. from tkinter import messagebox as mbox

我们导入messagebox,它具有显示对话框的功能。

  1. error = Button(self, text="Error", command=self.onError)

我们创建一个错误按钮,该按钮调用onError()方法。 在方法内部,我们显示错误消息对话框。

  1. def onError(self):
  2. mbox.showerror("Error", "Could not open file")

如果按下错误按钮,则会显示错误对话框。 我们使用showerror()函数在屏幕上显示对话框。 此方法的第一个参数是消息框的标题,第二个参数是实际消息。

Tkinter 中的对话框 - 图1

图:错误消息 dialog

Tkinter 颜色选择器

颜色选择器是用于选择颜色的对话框。

color_chooser.py

  1. #!/usr/bin/env python3
  2. """
  3. ZetCode Tkinter tutorial
  4. In this script, we use colorchooser
  5. dialog to change the background of a frame.
  6. Author: Jan Bodnar
  7. Last modified: April 2019
  8. Website: www.zetcode.com
  9. """
  10. from tkinter import Tk, Frame, Button, BOTH, SUNKEN
  11. from tkinter import colorchooser
  12. class Example(Frame):
  13. def __init__(self):
  14. super().__init__()
  15. self.initUI()
  16. def initUI(self):
  17. self.master.title("Color chooser")
  18. self.pack(fill=BOTH, expand=1)
  19. self.btn = Button(self, text="Choose Color",
  20. command=self.onChoose)
  21. self.btn.place(x=30, y=30)
  22. self.frame = Frame(self, border=1,
  23. relief=SUNKEN, width=100, height=100)
  24. self.frame.place(x=160, y=30)
  25. def onChoose(self):
  26. (rgb, hx) = colorchooser.askcolor()
  27. self.frame.config(bg=hx)
  28. def main():
  29. root = Tk()
  30. ex = Example()
  31. root.geometry("300x150+300+300")
  32. root.mainloop()
  33. if __name__ == '__main__':
  34. main()

我们有一个按钮和一个框架。 单击按钮,我们显示一个颜色选择器对话框。 我们将通过从对话框中选择一种颜色来更改框架的背景颜色。

  1. (rgb, hx) = colorchooser.askcolor()
  2. self.frame.config(bg=hx)

askcolor()函数显示对话框。 如果单击“确定”,则返回一个元组。 它是 RGB 和十六进制格式的颜色值。 在第二行中,我们使用返回的颜色值更改框架的背景颜色。

Tkinter 中的对话框 - 图2

图:颜色选择器

Tkinter 文件对话框

tkFileDialog对话框允许用户从文件系统中选择文件。

file_dialog.py

  1. #!/usr/bin/env python3
  2. """
  3. ZetCode Tkinter tutorial
  4. In this program, we use the
  5. tkFileDialog to select a file from
  6. a filesystem.
  7. Author: Jan Bodnar
  8. Last modified: April 2019
  9. Website: www.zetcode.com
  10. """
  11. from tkinter import Frame, Tk, BOTH, Text, Menu, END
  12. from tkinter import filedialog
  13. class Example(Frame):
  14. def __init__(self):
  15. super().__init__()
  16. self.initUI()
  17. def initUI(self):
  18. self.master.title("File dialog")
  19. self.pack(fill=BOTH, expand=1)
  20. menubar = Menu(self.master)
  21. self.master.config(menu=menubar)
  22. fileMenu = Menu(menubar)
  23. fileMenu.add_command(label="Open", command=self.onOpen)
  24. menubar.add_cascade(label="File", menu=fileMenu)
  25. self.txt = Text(self)
  26. self.txt.pack(fill=BOTH, expand=1)
  27. def onOpen(self):
  28. ftypes = [('Python files', '*.py'), ('All files', '*')]
  29. dlg = filedialog.Open(self, filetypes = ftypes)
  30. fl = dlg.show()
  31. if fl != '':
  32. text = self.readFile(fl)
  33. self.txt.insert(END, text)
  34. def readFile(self, filename):
  35. with open(filename, "r") as f:
  36. text = f.read()
  37. return text
  38. def main():
  39. root = Tk()
  40. ex = Example()
  41. root.geometry("300x250+300+300")
  42. root.mainloop()
  43. if __name__ == '__main__':
  44. main()

在我们的代码示例中,我们使用tkFileDialog对话框选择一个文件,并在Text小部件中显示其内容。

  1. self.txt = Text(self)

这是Text小部件,我们将在其中显示所选文件的内容。

  1. ftypes = [('Python files', '*.py'), ('All files', '*')]

这些是文件过滤器。 第一个仅显示 Python 文件,另一个显示所有文件。

  1. dlg = filedialog.Open(self, filetypes = ftypes)
  2. fl = dlg.show()

该对话框已创建并显示在屏幕上。 我们得到返回值,它是所选文件的名称。

  1. text = self.readFile(fl)

我们读取了文件的内容。

  1. self.txt.insert(END, text)

文本被插入到Text小部件中。

Tkinter 中的对话框 - 图3

图:文件对话框

在 Tkinter 教程的这一部分中,我们使用了对话框窗口。