如何使框架看起来像标签框架

如何使框架看起来像标签框架

本文介绍了TTK:如何使框架看起来像标签框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python ttk gui的创建很简单,并且大多数具有本机外观(win7).不过,我有一个小问题:

Python ttk gui creating is easy and has mostly native look and feel (win7). I am having a slight problem, though:

我想要一个看起来像ttk.LabelFrame但没有标签的框架.只是省略text选项将留下一个难看的空白.

I would like to have a frame that looks like ttk.LabelFrame but without label. Just omitting the text option will leave an ugly gap.

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

Also I can not get the ttk.Frame border to look like LabelFrame. Is there an elegant way of doing this? Bonus karma if this works on all/most windows versions above xp.

也许它可以与样式一起使用,但是样式LabelFrame属性似乎大多为空(style.element_options("border.relief")).也许我找错地方了.

Maybe it works with styles but the style LabelFrame properties seem mostly empty (style.element_options("border.relief")). Maybe I am looking in the wrong place.

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()

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

output on win7 with freshly downloaded python 3.2.3:

tcl版本:8.5.9

tcl version: 8.5.9

千伏级:8.5

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

There is python 2.6.6 installed on this machine, too (same problem). Each installation seems to be using the correct tk/tcl, though.

推荐答案

好的,似乎获得LabelFrame的一种解决方案是使用一个空的小部件代替文本.因此,请稍微修改示例的第一部分:

OK, seems like one solution to getting the LabelFrame without the gap is to use an empty widget in place of the text. So, modifying the first part of your example slightly:

# 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主题为我工作.

Seems to work for me with the regular Win7 theme.

这篇关于TTK:如何使框架看起来像标签框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:38