In this Python GUI article i want to show you creating Font Dialog in wxPython. so this class represents the font chooser dialog. and using that class you will a nice font chooser dialog for choosing fonts for your text.
What is Font Dialog in wxPython?
In wxPython Font Dialog is a dialog window that allows users to select font settings such as font family, style, size, and color. It provides an easy way for users to customize the appearance of text in their applications. Font Dialog is commonly used in applications where users need to choose font settings for text elements such as labels, buttons or text controls.
With Font Dialog users can:
- Choose a font family from a list of available fonts installed on their system.
- Select a font style such as bold, italic or underline.
- Specify the font size.
- Set the font color.
Python GUI Font Dialog in wxPython
Let’s create our example, This is the complete code for Python GUI Font Dialog in wxPython
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 52 53 |
import wx # Define the main frame class class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title=title, size=(800, 600)) # Set window icon self.SetIcon(wx.Icon('codeloop.png')) # Create instance of the panel class self.panel = MyPanel(self) # Define panel class class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) # Create button to open the font dialog self.button = wx.Button(self, label="Open Font Dialog", pos=(100, 100)) # Create text control widget self.textCtrl = wx.TextCtrl(self, size=(400, 300), style=wx.TE_MULTILINE, pos=(100, 150)) # Bind button click event to the onOpen method self.Bind(wx.EVT_BUTTON, self.onOpen) # Method to handle opening the font dialog def onOpen(self, event): # Create an instance of the font dialog dialog = wx.FontDialog(None, wx.FontData()) # Show font dialog and wait for user input if dialog.ShowModal() == wx.ID_OK: # Get font data from the dialog data = dialog.GetFontData() font = data.GetChosenFont() colour = data.GetColour() # Set the font and color to the text control self.textCtrl.SetFont(font) self.textCtrl.SetForegroundColour(colour) # Define application class class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="Codeloop.org - wxPython FontDialog") self.frame.Show() return True # Create instance of the application app = MyApp() # Start application event loop app.MainLoop() |
At the top we have created our MyFrame. this class is a top level window that inherits from wx.Frame and we create the object of our MyPanel class in here.
1 2 3 4 5 6 7 8 |
class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title, size = (800,600)) self.panel = MyPanel(self) |
This is our MyPanel class that inherits from wx.Panel and we create a button with textctrl in this class also we create the event binding in this class
1 2 3 4 5 6 7 8 9 10 |
class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) self.button = wx.Button(self, label = "Open Font Dialog", pos = (100,100)) self.textCtrl = wx.TextCtrl(self, size=(400, 300), style=wx.TE_MULTILINE, pos = (100,150)) self.Bind(wx.EVT_BUTTON, self.onOpen) |
And this is the method that we want to bind this with our button, when a user click on the button i want to open a Font Dialog.
1 2 3 4 5 6 7 8 9 10 |
def onOpen(self, event): dialog = wx.FontDialog(None, wx.FontData()) if dialog.ShowModal() == wx.ID_OK: data = dialog.GetFontData() font = data.GetChosenFont() colour = data.GetColour() self.textCtrl.SetFont(font) #self.textCtrl.SetBackgroundColour(colour) self.textCtrl.SetForegroundColour(colour) |
So the last class is MyApp class that inherits from wx.App. the OnInit() method is where you will most often create frame subclass objects. and start our main loop.That’s it. Once the application’s main event loop processing takes over, control passes to wxPython. Unlike procedural programs, a wxPython GUI program primarily responds to the events taking place around it, mostly determined by a human user clicking with a mouse and typing at the keyboard. When all the frames in an application have been closed, the app.MainLoop() method will return and the program will exit.
1 2 3 4 5 6 7 8 9 10 |
class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="Font Dialog") self.frame.Show() return True app = MyApp() app.MainLoop() |
So run the code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email
How can I pass this code to Tkinter?