本文介绍了Tkinter:更改菜单栏和标题栏颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改菜单栏和标题的颜色.

I would like to change the color of the menu bar and that of the title.

这是我想要的示例.

有可能吗?

推荐答案

举例说明您要做什么:

import tkinter as tk
from tkinter import ttk

class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry("400x200")
        self.configure(background='black')
        self.overrideredirect(1)
        self.attributes("-topmost", True)

    def startMove(self,event):
        self.x = event.x
        self.y = event.y

    def stopMove(self,event):
        self.x = None
        self.y = None

    def moving(self,event):
        x = (event.x_root - self.x)
        y = (event.y_root - self.y)
        self.geometry("+%s+%s" % (x, y))

    def exit(self):
        self.destroy()


def save():
    print ('save')
    return None
def add():
    print('add')
    return None



class MenuBar(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master, bd=1, relief='raised')
        self.master=master
        self.configure(background='black',
                       cursor='hand2')

        file = tk.Menubutton(self, text='File',
                             background='black',
                             foreground='white',
                             activeforeground='black',
                             activebackground='white'
                             )
        file_menu = tk.Menu(file,tearoff=0)
        file_menu.add_command(label='save', command=save,
                              background='black',
                              foreground='white',
                              activeforeground='black',
                              activebackground='white'
                              )

        file.config(menu=file_menu)
        file.pack(side='left')

        edit = tk.Menubutton(self, text='Edit',
                             background='black',
                             foreground='white',
                             activeforeground='black',
                             activebackground='white'
                             )
        edit_menu = tk.Menu(edit,tearoff=0)
        edit_menu.add_command(label='add', command=add,
                              background='black',
                              foreground='white',
                              activeforeground='black',
                              activebackground='white'
                              )

        edit.config(menu=edit_menu)
        edit.pack(side='left')

        close = tk.Button(self, text='X', command=lambda:root.exit(),
                          background='black',
                          foreground='white')
        close.pack(side='right')

def show():
    print('show')
    return None
def ex_it():
    print('exit')
    return None

class MainFrame(tk.LabelFrame):
    def __init__(self, master=None):
        tk.LabelFrame.__init__(self, master, bd=1, relief='raised', text='MainFrame', background='black', foreground='white')
        self.master=master
        self.note = tk.Label(self, text='Your typed chars appear here:',
                             background='black',
                             foreground='white',
                             )
        self.note.grid(column=0, row=0, columnspan=2, sticky='w')
        self.entry = ttk.Entry(self, style='My.TEntry')
        self.entry.grid(column=0,row=1,columnspan=3, sticky='ew')
        self.columnconfigure(0, weight=1)
        self.b_frame=tk.Frame(self, bg='black')
        self.b_frame.grid(column=0,row=2,sticky='w')
        self.sh_b = tk.Button(self.b_frame, text='Show', command=show)
        self.ex_b = tk.Button(self.b_frame, text='Exit', command=ex_it)
        self.sh_b.grid(column=0, row=0, sticky='w')
        self.ex_b.grid(column=1, row=0, sticky='w', padx=5)

root = App()

menubar = MenuBar(root)
menubar.pack(side='top', fill='x')

mainframe = MainFrame(root)
mainframe.pack(fill='both', expand=1)

menubar.bind("<Button-1>", root.startMove)
menubar.bind("<ButtonRelease-1>", root.stopMove)
menubar.bind("<B1-Motion>", root.moving)

style = ttk.Style(root)
style.element_create("plain.field", "from", "clam")
style.layout("My.TEntry",
             [('Entry.plain.field', {'children': [(
                 'Entry.background', {'children': [(
                     'Entry.padding', {'children': [(
                         'Entry.textarea', {'sticky': 'nswe'})],
                                       'sticky': 'nswe'})], 'sticky': 'nswe'})],
                                     'border':'2', 'sticky': 'nswe'})])
style.configure("My.TEntry",
                 foreground="white",
                 fieldbackground="grey")

root.mainloop()

玩得开心!

首先,我使用类创建了 3 个对象,它们如下所示:

First, I've created 3 objects using classes and they look like this:

应用lication/我们从 Tk() 获取的窗口

The Application / our window that inharets from Tk()

class App(tk.Tk):
    def __init__(self):
        super().__init__()

然后是来自 Frame 的菜单栏,看起来像:

Then the Menubar which inharets from Frame and looks like:

class MenuBar(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master, bd=1, relief='raised')

以及从 tkinter 的 LabeFrame 类中引入的 MainFrame:

and the MainFrame which inharets from the LabeFrame class of tkinter:

class MainFrame(tk.LabelFrame):
    def __init__(self, master=None):
        tk.LabelFrame.__init__(self, master, bd=1, relief='raised', text='MainFrame', background='black', foreground='white')

要了解有关类和 init 方法的更多信息 [点击]了解语法 self. [点击]

To learn more about classes and the init method [click]To understand the syntax self. [click]

让我们仔细看看应用程序:

Let's take a closer look to the App:

self.geometry("400x200")
self.configure(background='black')
self.overrideredirect(1)
self.attributes("-topmost", True)

  • 使用几何方法,我们定义了width=400height=200 以像素为单位.
  • 然后我们用一行来配置背景:
    • with the geometry method we are define the width=400 and height=200 in pixels.
    • Then we configure the background with the line:
    • self.configure(background='black')
      

      • 在此之后,我们使用 tkinter 的 overrideredirect 方法这显然是在做:
        • After this we use the overrideredirect method of tkinter which is clearly doing:
        • 设置或获取覆盖重定向标志.如果非零,这可以防止装饰窗口的窗口管理器.换句话说,窗口将没有标题或边框,并且不能移动或通过普通方式关闭.

          • 我们最终使用 属性方法 用于顶层并将最上面的参数设置为 true :
            • and we finally use the attributes method for Toplevels and set the argument topmost to true that does:
            • (Windows) 如果设置,这个窗口总是放在其他窗口之上视窗.请注意,在此版本中,此属性必须指定为-topmost".


              使用 overrideredirect 后最大的问题是您无法再移动窗口,因为窗口管理器不再有边框/标题或菜单栏.所以我们需要自己携带使用这个代码:


              The biggest problem after using overrideredirect is that you can't move your window anymore, cause there is no border/Title- or Menubar of the window manager anymore. So we need to carry it by ourself with using this code:

              def startMove(self,event):
                  self.x = event.x
                  self.y = event.y
              
              def stopMove(self,event):
                  self.x = None
                  self.y = None
              
              def moving(self,event):
                  x = (event.x_root - self.x)
                  y = (event.y_root - self.y)
                  self.geometry("+%s+%s" % (x, y))
              

              此代码的作用是通过使用 事件管理器

              And what this code does is to get the current mouse postion by clicking/Button-1 with the event manger

              event.x 或 event.y 表示:

              event.x or event.y means:

              当前鼠标位置,以像素为单位.

              event.x_root 或 event.y_root 表示:

              event.x_root or event.y_root means:

              当前鼠标相对于左上角的位置屏幕,以像素为单位.

              并通过从另一个中减去一个,我们得到偏移量,这是我们需要的 几何方法移动".

              and by substracting the one from the other we get the offset, that we need for our geometry method to "move".

              这篇关于Tkinter:更改菜单栏和标题栏颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:36
查看更多