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.
Also you can check More GUI Development Tutorials in the below link.
1: PyQt5 GUI Development Tutorials
2: TKinter GUI Development Tutorials
3: Pyside2 GUI Development Tutorials
4: Kivy GUI Development Tutorials
So now 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 |
import wx class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title, size = (800,600)) self.panel = MyPanel(self) 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) 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) class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="Font Dialog") self.frame.Show() return True app = MyApp() 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
Also you can watch the complete video tutorial for this article
Subscribe and Get Free Video Courses & Articles in your Email
How can I pass this code to Tkinter?