gpt4 book ai didi

python - 如何设计和自定义 tkinter/guizero 菜单栏?

转载 作者:行者123 更新时间:2023-12-04 02:34:14 33 4
gpt4 key购买 nike

我正在使用 Tkinter 和 guizero 并尝试设置菜单栏的样式。我正在使用 Python 3.8.2

我想改变的事情

  1. 移除菜单栏和选项的边框/3D 效果。

  2. 删除菜单栏顶部填充(上方和左侧的小空间)。

  3. 顶层/菜单栏和选项的事件选择颜色。

from guizero import App, MenuBar

def file_function():
print("File option")

def about_function():
print("about option")

app = App(title="My app", height=300, width=500,bg='white')
menubar = MenuBar(app,
toplevel=["File", "About"],
options=[
[ ["New", file_function], ["Save", file_function]],
[ ["Report Bug", about_function], ["About", about_function] ]
])

menubar.bg=(111, 77, 124)

# none of the styling below works and this is what I've tried
menubar.border=0
menubar.toplevel.border=False
menubar.options.border=0
menubar.toplevel.options.bg='gray'
menubar.toplevel.focus.bg='yellow'
menubar.toplevel.focus.fg='yellow'
menubar.toplevel.options.border=False
app.display()

图片:

enter image description here

更新

菜单并不意味着目前看起来不错,奇怪的颜色是为了看看什么能用,什么不能用。我能够使用 guizero 小部件及其所有功能。

当前的问题

  1. 无法在选择项目时移除菜单和子菜单上的 3d/可能的填充效果

我试过将 border 设置为 0 并将 highlightthickness 设置为 0

更新代码

from guizero import *
from tkinter import *

app=App(title='Test',bg=(53, 60, 81))
root = app.tk

def hello():
print ("hello!")


#creates menubar
menubar = Menu(root,relief=FLAT,bd=0)




# Sets menubar background color and active select but does not remove 3d effect/padding
menubar.config(bg = "GREEN",fg='white',activebackground='red',activeforeground='pink',relief=FLAT)


# First item on menubar and creates sub options
filemenu = Menu(menubar, tearoff=0,relief=FLAT, font=("Verdana", 12),activebackground='red')
filemenu.config(bg = "GREEN")
filemenu.add_command(label="New (Ctrl + N)", command=hello)
filemenu.add_command(label="Save(Ctrl + S)", command=hello)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)



# Adds to menubar and creates sub options
editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Cut", command=hello)
editmenu.add_command(label="Copy", command=hello)
editmenu.add_command(label="Paste", command=hello)
menubar.add_cascade(label="Edit", menu=editmenu)

helpmenu = Menu(menubar, tearoff=0,bg='green',fg='blue')

helpmenu.add_command(label="Report bug", command=hello)
helpmenu.add_command(label="About", command=hello)
menubar.add_cascade(label="Help", menu=helpmenu)

helpmenu.activebackground='red'



root.config(menu=menubar)
app.display()

最佳答案

我分解了以下代码。您确实需要先导入 tkinter,否则会抛出错误。

from tkinter import * 
from guizero import *


#background can be set rgb values,color name, and hex values
# width and height set the defualt startup window size
app=App(title='Test',bg=(53, 60, 81),width=500,height=500)

#sets min and max size of window
app.tk.minsize(width=250,height=250)
app.tk.maxsize(width=550,height=550)

root = app.tk


#Basic test function
def hello():
print ("hello!")




#creates the menubar and sets the location to root window
menubar = Menu(root,relief=FLAT,bd=0)


'''
bg sets the menubar background color
fg sets the text color
activebackground sets the selected item background
activeforeground set the selected item text color
active borderwidth removes the 3d effect/border around item
font sets the font type and size
Defualt text,background and other things can be set with variables

'''

menubar.config(bg = "GREEN",fg='white',activebackground='red',activeforeground='purple',activeborderwidth=0,font=("Verdana", 12))


# create a pulldown menu, and add it to the menu bar
# background,foreground and bother border and active border width needs to be set to remove any 3d border effect
filemenu = Menu(menubar, tearoff=0,relief='flat', bd=0,activebackground='red',activeborderwidth=0,font=("Verdana", 12))
filemenu.config(bg = "GREEN")
filemenu.add_command(label="New", command=hello)
filemenu.add_command(label="Save", command=hello)
#add line between drop down menu items
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
# sets the top level menu list name
menubar.add_cascade(label="File", menu=filemenu)





# create a pulldown menu, and add it to the menu bar
#example of no styling added to the sub menu
editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Cut", command=hello)
editmenu.add_command(label="Copy", command=hello)
editmenu.add_command(label="Paste", command=hello)
# sets the top level menu list name
menubar.add_cascade(label="Edit", menu=editmenu)

# create a pulldown menu, and add it to the menu bar
# show custom effects can be add to each sub menu
helpmenu = Menu(menubar, tearoff=0,bg='orange')
helpmenu.add_command(label="Report bug", command=hello)
helpmenu.add_command(label="About", command=hello)
# sets the top level menu list name
menubar.add_cascade(label="Help", menu=helpmenu)




# example of guizero widget
box = Box(app,height=200,width=500)
box.set_border(thickness=2, color='green')
box.bg=(53, 60, 81)
box.text_color='white'
exampel_text = Text(box, text="Hello World")
Picture(box,"example.png")


# display the menu and other things
root.config(menu=menubar)
app.display()

关于python - 如何设计和自定义 tkinter/guizero 菜单栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62601761/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com