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()

在这里插入图片描述

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()

在这里插入图片描述

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()

在这里插入图片描述

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()

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值