zoukankan      html  css  js  c++  java
  • [Python][小知识][NO.4] wxPython 字体选择对话框(O.O 不知道放到那里就放到这个分类的)

    1、前言

      O.O 前两天回家浪了两天,断更了 哎~~~

      o.o 有时候,有木有想改标签或编辑框中内容的字体呀?(o.o 反正我是没有)。

      wxpython也可以说是所在的操作系统,有字体选择器,给我们使用。

      其的样式风格是一定的。o.o 自绘一个也是可以得,比如那些大软件,(o.o 我想我应该不会去这么做)

      他的调用方法和步骤都比较简单。

    2、方法

      一下是一个绑定了菜单项目的一个菜单事件

      

      一般调用这种字体对话框的时候,基本就是上面这样的通用步骤。

      1、创建一个 字体选择对话框 对象

      2、设置 对话框 中选中的字体

        以上 3行代码 可以简化为: self.content.SetFont(dlg.GetFontData().GetChosenFont())

      3、板子完事

      PS.如果我们在 字体选择对话框中 更改字体颜色, 以下样例不支持。

        细心的童鞋坑定注意到了:我们只是设置了字体,并没有去把 data 中的颜色提取出来并进行设置。

        o.o 说了是板子,设置字体颜色 相关方法 奉上。

        获取 data 中的颜色:

        

        设置 字体颜色:

        哈哈  自己去查,完全伸手怎么行。

      效果图:

        

        

    3、样例代码

      

     1 # coding: utf-8
     2 # author: Twobox
     3 
     4 import wx
     5 
     6 class MyWin(wx.Frame):
     7     def __init__(self, parent, title):
     8         super(MyWin, self).__init__(parent=parent, title=title)
     9         self.initUI()
    10         self.Centre()
    11         self.Show()
    12 
    13     def initUI(self):
    14         """init Windows UI"""
    15         # init Windows Box
    16         panel = wx.Panel(self)
    17         vbox = wx.BoxSizer(wx.VERTICAL)
    18         self.content = wx.TextCtrl(panel, style=wx.TE_MULTILINE | wx.TE_DONTWRAP)
    19         vbox.Add(self.content, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
    20         panel.SetSizer(vbox)
    21 
    22         # init MenuBar
    23         menuBar = wx.MenuBar()
    24         setMenu = wx.Menu()
    25         fontMenuItem = wx.MenuItem(setMenu, id=11, text="字体(&F)...", kind=wx.ITEM_NORMAL)
    26         setMenu.Append(fontMenuItem)
    27         menuBar.Append(setMenu, title="设置(&S)")
    28         self.SetMenuBar(menuBar)
    29         self.Bind(wx.EVT_MENU, self.eventMenu)
    30 
    31     def eventMenu(self, event):
    32         id = event.GetId()
    33         if id == 11:
    34             dlg = wx.FontDialog(self, wx.FontData())
    35 
    36             if dlg.ShowModal() == wx.ID_OK:
    37                 data = dlg.GetFontData()
    38                 Font = data.GetChosenFont()
    39                 self.content.SetFont(Font)
    40 
    41             dlg.Destroy()
    42 
    43 def main():
    44     app = wx.App()
    45     MyWin(None, "FontDialog - Test")
    46     app.MainLoop()
    47 
    48 if __name__ == '__main__':
    49     main()

    4、后言

      一个 板子而已,套着用就可以了。

      转载请注明出处 (●'◡'●):http://www.cnblogs.com/Twobox/

      2017-08-30 00:15:02 -> 2017-08-30 00:37:45

  • 相关阅读:
    Roads in the Kingdom CodeForces
    Vasya and Shifts CodeForces
    SOS dp
    Singer House CodeForces
    Codeforces Round #419 (Div. 1) (ABCD)
    在踏踏实实的生活里让自己坚持去做梦
    Replace Delegation with Inheritance
    Replace Inheritance with Delegation
    Replace Parameter with Methods
    Preserve Whole Object
  • 原文地址:https://www.cnblogs.com/Twobox/p/7451042.html
Copyright © 2011-2022 走看看