python logo


Tag: dialog

wxPython dialogs

To display a dialog with wxPython requires only a few lines of code. We will demonstrate that below. We’ll discuss information dialog, simple dialog, error dialog, warning dialog and others.

Related course: Creating GUI Applications with wxPython

Information dialog
An information dialog can be shown with one line of code:

import wx

app = wx.App()
wx.MessageBox('Pythonspot wxWidgets demo', 'Info', wx.OK | wx.ICON_INFORMATION)

The first parameter is the actual text to display. The second is the title and final parameter tells wx to show the information icon and button.

Output:

wx dialog wx dialog

More dialogs: Warning dialog, Error dialog and default dialog
By modifying the parameters you can easily create other types of dailogs. An example below:

import wx

app = wx.App()

# simple dialog
wx.MessageBox('A dialog', 'Title', wx.OK)

# warning dialog
wx.MessageBox('Operation could not be completed', 'Warning', wx.OK | wx.ICON_WARNING)

# error dialog
wx.MessageBox('Operation could not be completed', 'Error', wx.OK | wx.ICON_ERROR)

Output (only one of the dialogs):

wxDialog wxDialog

Question dialog
Wx can be used to create a question dialog (yes/no). Example code:

import wx

app = wx.App()

dlg = wx.MessageDialog(None, "Do you want to update?",'Updater',wx.YES_NO | wx.ICON_QUESTION)
result = dlg.ShowModal()

if result == wx.ID_YES:
print "Yes pressed"
else:
print "No pressed"

Output:

wxDialog wxDialog

Related course: Creating GUI Applications with wxPython

Tkinter tkFileDialog module

tkFileDialog is a module with open and save dialog functions. Instead of implementing those in Tkinter GUI on your own.

This page is a collection of python tkinter file dialogs. The code is displayed here along with screenshots. Dialogs included in tkinter let you ask for a filename or directory.

tkinter file dialogs are easy to use, but not that easy to find out how to use. The main problem is that there is no simple example of how to do it. This article tries to fill the gap.

Related course:

Tkinter Open File

The askopenfilename function to creates an file dialog object. The extensions are shown in the bottom of the form (Files of type).

tkinter.tk.askopenfilename is an extension of the askopenfilename function provided in Tcl/Tk.

The code below will simply show the dialog and return the filename. If a user presses cancel the filename is empty. On a Windows machine change the initialdir to “C:\”.

Python 2.7 version:

from Tkinter import *from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog

root = Tk()
root.filename = tkFileDialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
print (root.filename)

Python 3 version:


from tkinter import filedialog
from tkinter import *

root = Tk()
root.filename = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
print (root.filename)

Here is an example (on Linux):

tkfiledialog Tkinter askopenfilename tkfiledialog Tkinter askopenfilename

Tkinter Save File

The asksaveasfilename function prompts the user with a save file dialog.

Python 2.7 version


from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog

root = Tk()
root.filename = tkFileDialog.asksaveasfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
print (root.filename)

Python 3 version


from tkinter import filedialog
from tkinter import *

root = Tk()
root.filename = filedialog.asksaveasfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
print (root.filename)

Tkinter Open Directory

Consider a scenario where your program needs to get a directory, the user clicked on the ok button and now you want to determine the path.

The askdirectory presents the user with a popup for directory selection.

Python 2.7 version


from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog
root = Tk()
root.directory = tkFileDialog.askdirectory()
print (root.directory)

tkinter-askdirectory tkinter askdirectory

If you are new to programming Tkinter, I highly recommend this course.

download tkinter examples