python - ttk:如何使框架看起来像标签框架?

标签 python tkinter frame ttk

Python ttk gui 的创建很容易,并且大部分具有原生的外观和感觉 (win7)。不过,我遇到了一个小问题:

我想要一个看起来像 ttk.LabelFrame 但没有标签的框架。仅仅省略文本选项会留下丑陋的空白。

我也无法让 ttk.Frame 边框看起来像 LabelFrame。有没有一种优雅的方式来做到这一点?如果这适用于 xp 以上的所有/大多数 Windows 版本,则奖励业力。

也许它适用于样式,但样式 LabelFrame 属性似乎大部分是空的 (style.element_options("border.relief"))。也许我找错地方了。

编辑:

try:  # python 3
    from tkinter import *  # from ... import * is bad
    from tkinter.ttk import *
except:
    from Tkinter import *
    from ttk import *

t = Tcl().eval
print("tcl version: " + str(t("info patchlevel")))
print("TkVersion: " + str(TkVersion))

root = Tk()

lF = LabelFrame(root, text=None)
lF.grid()
b = Button(lF, text='gonzo')
b.grid()

f = Frame(root, relief="groove") #GROOVE)
f.grid()
b2 = Button(f, text='gonzo')
b2.grid()

f2 = Frame(root, relief=GROOVE, borderwidth=2)
f2.grid()
b3 = Button(f2, text='gonzo')
b3.grid()

mainloop()

在 win7 上用新下载的 python 3.2.3 输出:

tcl 版本:8.5.9

Tk版本:8.5

output

这台机器上也安装了 python 2.6.6(同样的问题)。不过,每个安装似乎都使用了正确的 tk/tcl。

最佳答案

好吧,看起来没有间隙的 LabelFrame 的一种解决方案是使用空小部件代替文本。因此,稍微修改示例的第一部分:

# don't do the 'from tkinter import *' thing to avoid namespace clashes
import tkinter   # assuming Python 3 for simplicity's sake
import tkinter.ttk as ttk 

root = tkinter.Tk()

f = tkinter.Frame(relief='flat')
lF = ttk.LabelFrame(root, labelwidget=f, borderwidth=4)
lF.grid()
b = ttk.Button(lF, text='gonzo')
b.grid()

root.mainloop()

似乎适合我使用常规的 Win7 主题。

关于python - ttk:如何使框架看起来像标签框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12426315/

相关文章:

python - Tkinter 和随机数

android - 处理来自 Mediacodec 输出的帧并在 Android 上更新帧

cocoa - 在 Cocoa 中每帧绘制一次

python - 更新 txt 文件以检查 python 将数据保存到其中

python - PyQt5:如何向工作线程发送信号

python - OptionMenu 命令函数需要参数

Python Tkinter 只读文本字段

objective-c - 在第二个 View 中加载 Nib

python - Azure 机器学习工作室备份

python - 如何在 virtualenv 上安装 GExiv2?