我想个性化一个菜单栏。例如,我想删除 tk.Menu 小部件周围出现的边框(使用 add_command() 方法)
这是我的代码(我使用的是 Windows 10)
import tkinter as tk
from tkinter import ttk
dark_grey = "#212121"
dark_blue="#102A43"
blue_1="#243B53"
root = tk.Tk()
root.state('zoomed')
container = tk.Frame(root, bg = dark_grey)
container.grid_rowconfigure(0, weight = 0)
container.grid_columnconfigure(0, weight = 1)
menu_frame = tk.Frame(container, bg = dark_blue)
menu1 = tk.Menubutton(menu_frame, text = "Menu1", bg = dark_blue, fg =
"white", activebackground = blue_1, activeforeground =
"white")
menu1.grid(row = 0, column = 0)
submenu1 = tk.Menu(menu1, tearoff = 0, bg = dark_blue,
activebackground= blue_1, …Run Code Online (Sandbox Code Playgroud) 我想将我的tkinter应用程序的主题更改为clam.
什么是代码,我在哪里放?我试过了:
from tkinter import *
from tkinter.ttk import *
s=ttk.Style()
s.theme_use('clam')
Run Code Online (Sandbox Code Playgroud) 在以下代码中,该show_widget_validity()函数要么应用仅更改小部件现有样式的背景颜色的自定义样式,要么恢复原始样式。这是一个库例程,因此不能完全控制样式。背景颜色似乎已正确重新配置,如每次更改后条目小部件中报告的背景样式描述所示。但是,小部件的实际背景颜色不会改变。
此行为出现在使用 Python 2.7 和 3.6 的 Linux 上以及使用 Python 2.7 的 Windows 上;其他环境我没有测试过。
任何有关此行为原因的线索,或解释此行为所需的代码更改,将不胜感激。
编辑:使用“fieldbackground”代替“background”在Linux上有效,但在Windows上无效,并且不允许在禁用状态下修改背景颜色。
try:
import Tkinter as tk
except:
import tkinter as tk
try:
import ttk
except:
from tkinter import ttk
def show_widget_validity(widget, is_valid):
invalid_color = "#fff5ff"
invalid_disabled_color = "#f6f0f6"
sname = widget["style"] or widget.winfo_class()
if sname.startswith("Invalid."):
if is_valid:
widget['style'] = sname[8:]
else:
if not is_valid:
invname = "Invalid." + sname
ttk.Style().configure(invname, background=[('disabled', invalid_disabled_color), ('active', invalid_color)])
# Simpler, but also ineffective:
#ttk.Style().configure(invname, background=invalid_color)
widget['style'] …Run Code Online (Sandbox Code Playgroud) 我希望我的 gui 中的所有按钮都具有相同的样式。现在我手动写入我想要的属性,但它占用了太多空间。另外,如果我想改变样式,我必须去每一个按钮。是否可以定义一次样式,然后在制作所有按钮时引用它?类似于以下内容:
basic_style =
{'background': 'blue',
'foreground':'white',
'font':'Helvetica 8 bold'}
self.btn = tk.Button(text = 'Hello', style = basic_style)
Run Code Online (Sandbox Code Playgroud)
我知道可以做这样的事情:
self.btn['text'] = 'Bye'
Run Code Online (Sandbox Code Playgroud)
但这仍然对我没有多大帮助。
现在我有这三个代码:
tree["columns"] = ("one", "two", "three")
tree.column("one", width=150)
tree.column("two", width=150)
tree.column("three", width=150)
tree.heading("one", text="Naar")
tree.heading("two", text="Spoor")
tree.heading("three", text="Vetrektijd")
tree['show'] = 'headings'
Run Code Online (Sandbox Code Playgroud)
但我想要做的是将字体大小更改为三列中的 20,但是我该怎么做呢?因为在互联网上我读了一些关于 Style() 的东西,但这在我的代码中不起作用
我在互联网上找不到答案,所以我希望你能帮助我。我正在尝试从列表中打印到 Tkinter 中的文本框。由于某种原因,当我将它打印到文本框时,它没有按预期对齐,但是当我将它打印到控制台时,它正确对齐。
你们中有人知道可能是什么问题吗?
from tkinter import *
popup = Tk()
popup.wm_title("Podaci mreze")
widthTabela = 600
heightTabela = 500
def zatvaranje():
popup.destroy()
screenw = popup.winfo_screenwidth()
screenh = popup.winfo_screenheight()
x = screenw / 2 - widthTabela / 2
y = screenh / 2 - heightTabela / 2
popup.geometry("%dx%d+%d+%d" % (widthTabela, heightTabela, x, y))
textTFrame = Frame(popup, borderwidth=1, relief="sunken")
textTabela = Text(textTFrame, width=83, height=28.4, wrap="none", borderwidth=0)
textTVSB = Scrollbar(textTFrame, orient="vertical", command=textTabela.yview)
textTHSB = Scrollbar(textTFrame, orient="horizontal", command=textTabela.xview) …Run Code Online (Sandbox Code Playgroud)