wxPython:更改标题的颜色

时间:2018-06-23 10:35:45

标签: python user-interface wxpython

我想知道是否有一种方法可以在wxPython中更改窗口这一部分的背景颜色:

window's header

我已经知道如何更改标题或图标,但没有找到任何颜色。

我在Windows 10上使用Python 3.6。

1 个答案:

答案 0 :(得分:1)

简短的回答是“否” 。 WxPython使用操作系统的本机图形元素以及按钮,窗口等。

另一方面,您始终可以创建自己的小部件。

在下面的示例中,我创建了自定义wxFrame并删除了默认情况下使用窗口样式带来的标题栏。然后,通过尺寸调整器,我模拟了自己的标题栏及其图标,文本和按钮。困难的部分是通过使用鼠标拖动来移动窗口,但是Internet上有成千上万个示例。

我给你举个例子:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import wx

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = kwds.get("style", 0)
        wx.Frame.__init__(self, *args, **kwds)

        self.SetSize((400, 299))

        # Creting the custom title bar
        self.panelTitleBar = wx.Panel(self, wx.ID_ANY)
        self.btnMinimize = wx.Button(self.panelTitleBar, wx.ID_ANY, "", style=wx.BORDER_NONE | wx.BU_NOTEXT)
        self.btnMaximize = wx.Button(self.panelTitleBar, wx.ID_ANY, "", style=wx.BORDER_NONE | wx.BU_NOTEXT)
        self.btnExit = wx.Button(self.panelTitleBar, wx.ID_ANY, "", style=wx.BORDER_NONE | wx.BU_NOTEXT)
        self.panelBody = wx.Panel(self, wx.ID_ANY)

        self.Bind(wx.EVT_BUTTON, self.OnBtnExitClick, self.btnExit)
        self.Bind(wx.EVT_BUTTON, self.OnBtnMinimizeClick, self.btnMinimize)
        self.Bind(wx.EVT_BUTTON, self.OnBtnMaximizeClick, self.btnMaximize)
        self.panelTitleBar.Bind(wx.EVT_LEFT_DOWN, self.OnTitleBarLeftDown)        
        self.panelTitleBar.Bind(wx.EVT_MOTION, self.OnMouseMove) 


        self._isClickedDown = False;
        self._LastPosition = self.GetPosition();


        self.__set_properties()
        self.__do_layout()        

    def __set_properties(self):        
        self.SetTitle("frame")
        self.btnMinimize.SetMinSize((22, 22))
        self.btnMinimize.SetBitmap(wx.Bitmap("images\\btn_minimize.png", wx.BITMAP_TYPE_ANY))
        self.btnMaximize.SetMinSize((22, 22))
        self.btnMaximize.SetBitmap(wx.Bitmap("images\\btn_maximize.png", wx.BITMAP_TYPE_ANY))
        self.btnExit.SetMinSize((22, 22))
        self.btnExit.SetBitmap(wx.Bitmap("images\\btn_close.png", wx.BITMAP_TYPE_ANY))
        self.panelTitleBar.SetBackgroundColour(wx.Colour(44, 134, 179))
        self.panelBody.SetBackgroundColour(wx.Colour(255, 255, 255))        

    def __do_layout(self):

        #Sizers:
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_1 = wx.FlexGridSizer(2, 1, 0, 0)
        sizerTitleBar = wx.FlexGridSizer(1, 5, 0, 0)

        #Titlebar:
        iconTitleBar = wx.StaticBitmap(self.panelTitleBar, wx.ID_ANY, wx.Bitmap("[![enter image description here][1]][1]images\\tab03.ico", wx.BITMAP_TYPE_ANY))
        sizerTitleBar.Add(iconTitleBar, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT, 5)
        title = wx.StaticText(self.panelTitleBar, wx.ID_ANY, "My Window Title")
        title.SetForegroundColour(wx.Colour(255, 255, 255))
        title.SetFont(wx.Font(9, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, 0, ""))
        sizerTitleBar.Add(title, 0, wx.ALIGN_CENTER_VERTICAL | wx.BOTTOM | wx.TOP, 10)
        sizerTitleBar.Add(self.btnMinimize, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT, 2)
        sizerTitleBar.Add(self.btnMaximize, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT, 2)
        sizerTitleBar.Add(self.btnExit, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT, 2)
        sizerTitleBar.AddGrowableRow(0)
        sizerTitleBar.AddGrowableCol(1)

        self.panelTitleBar.SetSizer(sizerTitleBar)
        grid_sizer_1.Add(self.panelTitleBar, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.panelBody, 1, wx.EXPAND, 0)
        grid_sizer_1.AddGrowableRow(1)
        grid_sizer_1.AddGrowableCol(0)
        sizer_1.Add(grid_sizer_1, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        self.Layout()

    def OnTitleBarLeftDown(self, event):            
        self._LastPosition = event.GetPosition()            

    def OnBtnExitClick(self, event):
        self.Close()

    def OnBtnMinimizeClick(self, event):
        self.Iconize( True )


    def OnBtnMaximizeClick(self, event):    
        self.Maximize(not self.IsMaximized())

    def OnMouseMove(self, event):        
        if event.Dragging():
            mouse_x, mouse_y = wx.GetMousePosition()                                
            self.Move(mouse_x-self._LastPosition[0],mouse_y-self._LastPosition[1])



class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, wx.ID_ANY, "")
        self.SetTopWindow(self.frame)
        self.frame.Show()
        self.frame.Center()
        return True


if __name__ == "__main__":
    app = MyApp(0)
    app.MainLoop()

enter image description here

此自定义窗口可以移动,最大化,最小化和关闭。

我希望这会有所帮助。

PS 为了无错地执行上述代码,您需要将这4张图像放置在同一级别的“图像”文件夹中。 < / p>

enter image description here enter image description here enter image description here enter image description here