下拉选择框 (OptionMenu)

  1. # 创建组件
  2. def createWidget(self):
  3. # 接收选择值的变量, 选中的值会在这里
  4. self.v = tk.StringVar()
  5. self.v.set('请选择')
  6. self.om = tk.OptionMenu(self, self.v, 'A', 'B', 'C')
  7. self.om['width'] = 10
  8. self.om.pack()

ttk 中的下拉选择框

  1. from tkinter import ttk
  2. ttk.Combobox(master)
value 插入下拉选项
.current() 默认显示的下拉选项框
.get() 获取下拉选项框中的值
.insert() 下拉框中插入文本
.delete() 删除下拉框中的文本
state 下拉框的状态,分别包含DISABLED/NORMAL/ACTIVE
width 下拉框高度
foreground 前景色
selectbackground 选择后的背景颜色
fieldbackground 下拉框颜色
background 下拉按钮颜色

滑块

  1. # 创建组件
  2. def createWidget(self):
  3. self.s1 = tk.Scale(self, {
  4. # 最小值
  5. 'from_': 10,
  6. # 最大值
  7. 'to': 50,
  8. 'length': 200,
  9. # 显示区间(每隔多少显示)
  10. 'tickinterval': 5,
  11. # 方向横向, 默认纵向
  12. 'orient': 'horizontal',
  13. 'command': self.val
  14. }).pack()
  15. def val(self, value):
  16. print('滑块的值' + value)

颜色选择框

  1. # 创建组件
  2. def createWidget(self):
  3. tk.Button(self, {
  4. 'text': '选择颜色',
  5. 'command': self.color
  6. }).pack()
  7. def color(self):
  8. # 调出颜色选择器
  9. self.s = tk_color.askcolor(color='red', title='选择颜色')
  10. # 颜色选择结果
  11. print(self.s)

文件选择

  1. # 创建组件
  2. def createWidget(self):
  3. tk.Button(self, {
  4. 'text': '选择文件',
  5. 'command': self.selectFile
  6. }).pack()
  7. def selectFile(self):
  8. # 打开文件选择器
  9. self.f = tk_file.askopenfilename(title='上传文件', initialdir='~/', filetypes=[('PNG图片', '.png')])
  10. # 路径
  11. print(self.f)

对话框

简单数字对话框

  1. # 创建组件
  2. def createWidget(self):
  3. tk.Button(self, {
  4. 'text': '数字对话',
  5. 'command': self.selectFile
  6. }).pack()
  7. def selectFile(self):
  8. self.a = tk_sim.askinteger('数字输入对话框', prompt='请输入一个数字', initialvalue=18, minvalue=1, maxvalue=150)
  9. print(self.a)

其他输入对话框

image.png

其他消息对话框

image.png