python字体设置对话框_Python图形化界面入门教程 - wxPython创建字体对话框

在这篇Python GUI文章中,我们将向您展示在wxPython中创建字体对话框。这个类表示字体选择器对话框。使用这个类,你会有一个很好的字体选择对话框来为你的文本选择字体。

下面是使用Python的GUI库wxPython创建字体对话框的完整代码。

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 = "打开字体对话框", pos = (100,100))

self.textCtrl = wx.TextCtrl(self, size=(600, 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="www.linuxmi.com - 字体对话框")

self.frame.Show()

return True

app = MyApp()

app.MainLoop()

首先,我们创建了MyFrame。这个类是一个从wx继承的顶级窗口框架,我们在这里创建了MyPanel类的对象

class MyFrame(wx.Frame):

def __init__(self, parent, title):

super(MyFrame, self).__init__(parent, title =title, size = (800,600))

self.panel = MyPanel(self)​

这是我们的MyPanel类,它继承了wx.Panel,我们用textctrl在这个类中创建了一个按钮,也在这个类中创建了事件绑定

class MyPanel(wx.Panel):

def __init__(self, parent):

super(MyPanel, self).__init__(parent)

self.button = wx.Button(self, label = "打开字体对话框", pos = (100,100))

self.textCtrl = wx.TextCtrl(self, size=(600, 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)

最后一个类是MyApp类继承自wx.App。OnInit()方法通常是创建框架子类对象(frame subclass objects)。

然后开始我们的主循环(main loop)。就是这样。一旦应用程序的主事件循环处理接管,控制权就传递给wxPython。与过程性程序不同,wxPython GUI程序主要响应在其周围发生的事件,这些事件主要由用户用鼠标单击和键盘输入决定。当应用程序中的所有帧都被关闭时,app.MainLoop()方法将返回,程序将退出。

class MyApp(wx.App):

def OnInit(self):

self.frame = MyFrame(parent=None, title="www.linuxmi.com - 字体对话框")

self.frame.Show()

return True

app = MyApp()

app.MainLoop()

作者:聆听世界的鱼

来源:微信公众号-Linux公社

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值