首页 > 编程知识 正文

python怎么制作对话框(python 对话框的创建及调用)

时间:2023-11-29 19:12:35 阅读:310289 作者:WHNW

本文目录一览:

  • 1、实现Python打开对话框的问题
  • 2、如何用python编写弹出对话框,并选择yes/no
  • 3、python中选择文件夹(即路径)的对话框如何实现
  • 4、python 弹出式对话框

实现Python打开对话框的问题

可以使用tkinter.filedialog模块中的askopenfilename函数来实现,tkinter是python自带的GUI,通过askopenfilename函数打开选择文件对话框,代码如下:

import tkinter.filedialog

fn=tkinter.filedialog.askopenfilename(title='选择一个文件', filetypes=[('所有文件','.*'),('文本文件','.txt')])

print(fn)

效果如下:

函数说明:

askopenfilename(**options)

Ask for a filename to open

title参数设置标题, filetypes参数设置文件类型

如何用python编写弹出对话框,并选择yes/no

如果使用 python 自带的 tkinter 库 是这样实现的。

其他库的话可以查看一下api。

from Tkinter import *

from tkMessageBox import *

def answer():

    showerror("Answer", "Sorry, no answer available")

def callback():

    if askyesno('Verify', 'Really quit?'):

        showwarning('Yes', 'Not yet implemented')

    else:

        showinfo('No', 'Quit has been cancelled')

Button(text='Quit', command=callback).pack(fill=X)

Button(text='Answer', command=answer).pack(fill=X)

mainloop()

python中选择文件夹(即路径)的对话框如何实现

1、首先,确保我们已经正确安装了python2.7的环境,然后,编辑一个.py文件。

2、然后,我们用python运行下该文件,可以看到,界面首先弹出了一个选择文件的对话框,符合我们预期。

3、然后,我们在里面选择一个文件,如图所示,这里我们选择了某Word文档。

4、然后,点击【确定】后,可以看到黑色的界面上,打印输出了一段文字:C:/Users/用户名/Desktop/JAVA设计模式总结之23种设计模式.docx,符合预期。

5、然后,又会弹出一个选择文件夹的对话框,这里选择【桌面】,点击【确定】按钮。

6、最后,界面上有输出了:C:/Users/用户名/Desktop,符合我们的预期。

python 弹出式对话框

不知道你用的什么版本,我修改了一下,测试通过(python2.7):

# coding=utf-8

import Tkinter

import tkMessageBox

def show():

    tkMessageBox.showinfo(title='aaa', message='bbb')

def creatfram():

    root = Tkinter.Tk()

    b = Tkinter.Button(root, text="关于", command=show)

    b.pack()

    root.mainloop()

creatfram()如果解决了您的问题请采纳!

如果未解决请继续追问

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。