ttk: 如何使一个框架看起来像一个标签框架?[英] ttk: how to make a frame look like a labelframe?

本文是小编为大家收集整理的关于ttk: 如何使一个框架看起来像一个标签框架?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

tkversion: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主题对我有用.

本文地址:https://www.itbaoku.cn/post/2091148.html

问题描述

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

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.

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.

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.

edit:

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

output on win7 with freshly downloaded python 3.2.3:

tcl version: 8.5.9

TkVersion: 8.5

output

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

推荐答案

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

Seems to work for me with the regular Win7 theme.