如何在python&#t; tkinter 8.5中将ttk.LabelFrame的蓝色标题标签更改为黑色

时间:2014-07-28 19:00:14

标签: python tkinter ttk

我在Windows 7机器上使用tkinter 8.5和python 3.3。

下面的代码以蓝色字体呈现Labelframe的标题。

from tkinter import *
from tkinter import ttk

root = Tk()

lf = ttk.LabelFrame(root, text="Why is this blue?")
lf.pack()

label = ttk.Label(lf, text="label")
label.pack()

我尝试通过添加ttk.Style()来修复此问题,但出现了意外的显示:

from tkinter import *
from tkinter import ttk

root = Tk()

s = ttk.Style()
s.configure('TLabelframe.Label', font='arial 14 bold')

lf = ttk.LabelFrame(root, text="Now it's black, but w/ a bizarre display"
                    " and no etched frame.", style='TLabelframe.Label')
lf.pack()

label = ttk.Label(lf, text="label")
label.pack()

有没有办法让ttk.LabelFrame标题显示黑色并带有奇怪的副作用?

3 个答案:

答案 0 :(得分:4)

Windows似乎将ttk.Labelframe标头默认为此蓝色。不知道为什么。

Mac and Linux default to black, while Windows defaults to blue.

我通过创建一个ttk.Label并将其作为ttk.Labelframe的labelwidget参数传递给我找到了一个解决方案。不过,这可能更像是一种解决方法。无论如何,下面的代码在我的Windows 7机器上以黑色显示标题文本。

from tkinter import *
from tkinter import ttk

root = Tk()

l = ttk.Label(text="Not blue anymore")

lf = ttk.Labelframe(root, labelwidget=l)
lf.pack()

label = ttk.Label(lf, text="label")
label.pack()

root.mainloop()

答案 1 :(得分:0)

这也可以使用前景属性tk.LabelFrame(window, foreground='red')

进行更改

答案 2 :(得分:0)

您只需要从from subprocess import Popen, PIPE import sys import os from PyQt5.QtCore import Qt from PyQt5.QtWidgets import (QApplication, QDialog, QVBoxLayout, QHBoxLayout, QLabel, QProgressBar) def has_bash(): """ Test if bash is available. If present the string `/bin/bash` is returned, an empty string otherwise. """ res = Popen( ["which", "bash"], stdout=PIPE, stderr=PIPE, text="utf-8" ) out, _ = res.communicate() shell = out.strip() return shell def run_script(command): """ Run the script and catch output of the subprocess line by line. The `command` argument is set in `run_pip()`. """ process = Popen(command, stdout=PIPE, text="utf-8") while True: output = process.stdout.readline() if output == "" and process.poll() is not None: break if output: # TODO: show output in dialog together with a progressbar print(f"[PIP]: {output.strip()}") rc = process.poll() return rc def run_pip(cmd, opt, package, venv_dir, venv_name): """ Activate the virtual environment and run pip commands. """ current_dir = os.path.dirname(os.path.realpath(__file__)) script = os.path.join(current_dir, "run.sh") if has_bash(): # create run script with open(script, "w") as f: f.write( "#!/bin/bash\n" f"source {venv_dir}/{venv_name}/bin/activate\n" f"pip {cmd}{opt}{package}\n" "deactivate\n" ) # make it executable os.system(f"chmod +x {script}") # run script command = ["/bin/bash", script] run_script(command) class ProgBarDialog(QDialog): """ Dialog showing output and a progress bar during the installation process. """ def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(675, 365, 325, 80) self.setFixedSize(350, 85) self.setWindowFlag(Qt.WindowCloseButtonHint, False) self.setWindowFlag(Qt.WindowMinimizeButtonHint, False) h_Layout = QHBoxLayout(self) v_Layout = QVBoxLayout() h_Layout.setContentsMargins(0, 15, 0, 0) self.statusLabel = QLabel(self) self.placeHolder = QLabel(self) self.progressBar = QProgressBar(self) self.progressBar.setFixedSize(325, 23) self.progressBar.setRange(0, 0) v_Layout.addWidget(self.statusLabel) v_Layout.addWidget(self.progressBar) v_Layout.addWidget(self.placeHolder) h_Layout.addLayout(v_Layout) self.setLayout(h_Layout) if __name__ == "__main__": cmd = ["install "] opt = ["--upgrade "] package = "pylint" # this could be any package current_dir = os.path.dirname(os.path.realpath(__file__)) venv_name = "testenv" # a virtual env beside this test file run_pip(cmd[0], opt[0], package, current_dir, venv_name) #]=======================================================================[# app = QApplication(sys.argv) progBar = ProgBarDialog() progBar.show() sys.exit(app.exec_()) 选项中删除style='TLabelframe.Label'

ttk.LabelFrame

我正在使用Python 3.7.3和tkinter 8.6。