当前位置:   article > 正文

Python桌面开发tkinter—对话框与选择框_python tkinter选择框

python tkinter选择框

messagebox消息框

语法

  • messagebox.askquestion(title,message):返回yesno
    在这里插入图片描述

  • messagebox.askokcancel(title,message):返回TrueFalse
    在这里插入图片描述

  • messagebox.askretrycancel(title,message):返回TrueFalse
    在这里插入图片描述

  • messagebox.askyesno(title,message):返回TrueFalse

在这里插入图片描述

  • messagebox.showinfo(title,message):返回ok
    在这里插入图片描述

  • messagebox.showwarning(title,message):返回ok
    在这里插入图片描述

  • messagebox.showerror(title,message):返回ok
    在这里插入图片描述

示例

import tkinter as tk
from tkinter import messagebox

root = tk.Tk()
root.geometry("500x300")

def okcancel():
    result = messagebox.askokcancel(title='askokcancel', message='这是askokcancel')
    print(result)

def question():
    result = messagebox.askquestion(title='askquestion', message="这是askquestion")
    print(result)

def retrycancel():
    result = messagebox.askretrycancel(title='askretrycancel', message="这是askretrycancel")
    print(result)

def yesno():
    result = messagebox.askyesno(title='askyesno', message="这是askyesno")
    print(result)

def error():
    result = messagebox.showerror(title='showerror', message="这是showerror")
    print(result)

def warning():
    result = messagebox.showwarning(title='showwarning', message="这是showwarning")
    print(result)

def info():
    result = messagebox.showinfo(title='showinfo', message="这是showinfo")
    print(result)

button_0 = tk.Button(root, text="askokcancel", command=okcancel)  # command的函数不带圆括号
button_0.pack()
button_1 = tk.Button(root, text="askquestion", command=question)
button_1.pack()
button_2 = tk.Button(root, text="askretrycancel", command=retrycancel)
button_2.pack()
button_3 = tk.Button(root, text="askyesno", command=yesno)
button_3.pack()
button_4 = tk.Button(root, text="showerror", command=error)
button_4.pack()
button_5 = tk.Button(root, text="showwarning", command=warning)
button_5.pack()
button_6 = tk.Button(root, text="showinfo", command=info)
button_6.pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

在这里插入图片描述

simpledialog输入框

语法

  • simpledialog.askstring(title, prompt, initialvalue):必须输入字符串,initialvalue为输入框的初始默认值

    在这里插入图片描述

  • simpledialog.askinteger(title, prompt, initialvalue):必须输入整数

    在这里插入图片描述

  • simpledialog.askfloat(title, prompt, initialvalue):必须输入浮点型
    在这里插入图片描述

示例

import tkinter as tk
from tkinter import simpledialog

root = tk.Tk()
root.geometry("500x300")

def askstring():
    res = simpledialog.askstring(title="askstring", prompt="这是askstring", initialvalue='initialvalue')
    print(res)

def askinteger():
    res = simpledialog.askinteger(title="askinteger", prompt="这是askinteger")
    print(res)

def askfloat():
    res = simpledialog.askfloat(title="askfloat", prompt="这是askfloat")
    print(res)

button_0 = tk.Button(root, text='askstring', command=askstring)
button_0.pack()
button_1 = tk.Button(root, text='askinteger', command=askinteger)
button_1.pack()
button_2 = tk.Button(root, text='askfloat', command=askfloat)
button_2.pack()


root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

在这里插入图片描述

filedialog文件框

在这里插入图片描述

语法

  • filedialog.askopenfile([mode='r',title,initialdir,initialfile]):
    • 打开单个文件
    • 返回值:打开文件获取文件指针
  • filedialog.askopenfiles([mode='r',title,initialdir,initialfile]):
    • 打开多个或单个文件
    • 返回值:列表,包含选取的文件的指针
  • filedialog.askopenfilename([title,initialdir,initialfile])
    • 打开单个文件
    • 返回值:绝对路径。
    • 适用于用户想要选择现有文件的情况。如果选择了一个不存在文件,则会出现一个弹出窗口,通知所选的文件不存在
  • filedialog.askopenfilenames([title,initialdir,initialfile])
    • 选择多个文件
    • 返回值:元组,包含其路径
  • filedialog.asksaveasfilename([title,initialdir,initialfile])
    • 适应于用户想要创建新文件或替换现有文件的情况
    • 如果选择现有文件,则会出现一个弹出窗口,通知所选的文件已存在,并询问是否确认替换
  • filedialog.askdirectory([title,initialdir,initialfile])
    • 选取文件夹(目录)
    • 返回值:目录的绝对路径

参数:

  • title:对话框标题
  • initialdir:默认初始路径
  • initialfile:默认初始文件

示例

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
root.geometry("500x300")

# 以下两个方法获取文件路径
def openfilename():
    # initialdir: 默认路径
    # initialfile: 默认文件
    path = filedialog.askopenfilename(initialdir="D:/", initialfile='Python')
    print(path)

def openfilenames():
    # 参数title:设置标题
    paths = filedialog.askopenfilenames(title="打开心意的文件,亲!")
    print(paths)

# 以下两个方法获取文件地址的指针
def openfile():
    # 参数mode默认是只读'r'的方式,不可修改
    fp = filedialog.askopenfile()
    print(fp)

def openfiles():
    fps = filedialog.askopenfiles()
    print(fps)

def directory():
    dir = filedialog.askdirectory()
    print(dir)

# 此函数只负责提示保存的路径,而不会真正保存文件,除非使用os模块
def saveasfilename():
    path = filedialog.asksaveasfilename()
    print(path)

button_0 = tk.Button(root, text="openfilename", command=openfilename)
button_0.pack()
button_1 = tk.Button(root, text="openfilenames", command=openfilenames)
button_1.pack()
button_2 = tk.Button(root, text="openfile", command=openfile)
button_2.pack()
button_3 = tk.Button(root, text="openfiles", command=openfiles)
button_3.pack()
button_4 = tk.Button(root, text="dirctory", command=directory)
button_4.pack()
button_5 = tk.Button(root, text="saveasfilename", command=saveasfilename)
button_5.pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

在这里插入图片描述

colorchooser颜色选取框

在这里插入图片描述

语法

  • res = colorchooser.askcolor([color])
    • 参数: color,默认初始选中的颜色
    • 返回值:元组,选中颜色的16进制值的RGB值
import tkinter as tk
from tkinter import colorchooser

root = tk.Tk()
root.geometry("500x300")

def color_chose():
    # color: 设置默认颜色值
    res = colorchooser.askcolor(color='red')
    print(res)
    # 设置界面的背景颜色
    root['bg'] = res[1]

button = tk.Button(root, text="color", command=color_chose)
button.pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

在这里插入图片描述在这里插入图片描述

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号