问题描述
我实际上是在尝试从 tkinter.Spinbox
创建 ttk.Spinbox
.我可以像 ttk.Scrollbar
模式一样操作下面的代码.tkinter.Spinbox
按钮让我的 GUI 看起来很旧,这就是我想要 ttk.Spinbox
的原因.
我在 Windows 7 操作系统上使用 Python 3.4
.我需要一个主题的 Spinbox 小部件.ttk.__init__
文件没有 Spinbox 类/模块.因此,我打开该文件并编写代码,就像下面给出的 Scrollbar
类一样.
class Scrollbar(Widget, tkinter.Scrollbar):"""Ttk Scrollbar 控制可滚动小部件的视口."""def __init__(self, master=None, **kw):"""用父母版构造一个Ttk Scrollbar.标准选项类,光标,样式,takefocus小部件特定选项命令,方向"""Widget.__init__(self, master, "ttk::scrollbar", kw)
以及我放入 ttk.__init__.py
文件中的代码.它继承自 tkinter.Spinbox
.
class Spinbox(Widget, tkinter.Spinbox):"""旋转框小部件."""def __init__(self, master=None, **kw):Widget.__init__(self, master, "ttk::spinbox", kw)
下面给出的测试结果是令人满意的.但是在Spinbox Widget
tkinter.Spinbox 小部件
ttk.Spinbox 小工具
有什么问题吗?为什么会出现缩进?
导入 tkinter 作为 tk将 tkinter.ttk 导入为 ttk类 Spinbox(ttk.Widget):def __init__(self, master, **kw):ttk.Widget.__init__(self, master, 'ttk::spinbox', kw)如果 __name__ == '__main__':根 = tk.Tk()root.grid_columnconfigure(0, weight=1)opts = { 'from_': 5, 'to': 10, 'increment': 1 }sp1 = tk.Spinbox(root, from_=5, to=10)sp1.grid(行=0,列=0)sp2 = Spinbox(root, from_=5, to=10)sp2.grid(row=1, column=0, columnspan=2,sticky="we",pady=2)root.mainloop()
如果您展开根窗口,ttk.Spinbox
会随之展开并出现缩进.我想这是由于 columnconfigure
但我需要配置以获得更好的外观.
这似乎是 Tk 本身的一个错误,而您的 Python 代码只是暴露了它.我将您的示例代码直接转换为 Tcl/Tk 并针对 Tk-8.5.17 和 Tk-8.6.3 运行它(通过
问题代码似乎在 ttk
子目录中的 vistaTheme.tcl
文件中,在以 ttk::style layout TSpinbox
:
ttk::style 布局 TSpinbox {Spinbox.field -sticky nswe -children {Spinbox.background -sticky news -children {Spinbox.padding -sticky news -children {Spinbox.innerbg -sticky news -children {Spinbox.textarea -expand 1 -sticky {}}}Spinbox.uparrow -side top -sticky ensSpinbox.downarrow -side 底部 -sticky ens}}}
具体来说,如果您从最内层块的 Spinbox.textarea -expand 1 -sticky {}
中删除 -sticky {}
位,那么看起来缩进离开:
我建议通读 Tcl/Tk 错误 wiki 此处,然后与他们一起打开错误 此处 了解此问题.不过不会屏住呼吸.Tcl/Tk 发布并不经常发生,因为它是一种非常成熟和稳定的语言.如果为 Tcl/Tk 生成了修复程序,那么您需要向 Python 维护人员提交错误,让他们更新 Windows 版本的 Tcl/Tk 内部副本或向后移植修复程序.
通过使用 ttk 的样式并配置 Spinbox.textarea
位以取消设置粘性属性,可能可以解决 Python 中的问题,但是,我没有确切的代码片段现在就这样做.
I'm actually trying to create ttk.Spinbox
from tkinter.Spinbox
. I can manipulate codes below like ttk.Scrollbar
pattern. tkinter.Spinbox
button gives an old look for my GUI that is why i want to ttk.Spinbox
.
Edit:I am using Python 3.4
on Windows 7 OS. I need a themed Spinbox widget. ttk.__init__
file has not Spinbox class/module. So, I open that file and wrote codes just like Scrollbar
class given below.
class Scrollbar(Widget, tkinter.Scrollbar):
"""Ttk Scrollbar controls the viewport of a scrollable widget."""
def __init__(self, master=None, **kw):
"""Construct a Ttk Scrollbar with parent master.
STANDARD OPTIONS
class, cursor, style, takefocus
WIDGET-SPECIFIC OPTIONS
command, orient
"""
Widget.__init__(self, master, "ttk::scrollbar", kw)
And codes that I placed into ttk.__init__.py
file. It inherits from tkinter.Spinbox
.
class Spinbox(Widget, tkinter.Spinbox):
"""spinbox widget."""
def __init__(self, master=None, **kw):
Widget.__init__(self, master, "ttk::spinbox", kw)
The test result given below is satisfactory.But there is an indent before content of Spinbox Widget
tkinter.Spinbox Widget
ttk.Spinbox Widget
Is there something wrong? Why an indentation occurs?
import tkinter as tk
import tkinter.ttk as ttk
class Spinbox(ttk.Widget):
def __init__(self, master, **kw):
ttk.Widget.__init__(self, master, 'ttk::spinbox', kw)
if __name__ == '__main__':
root = tk.Tk()
root.grid_columnconfigure(0, weight=1)
opts = { 'from_': 5, 'to': 10, 'increment': 1 }
sp1 = tk.Spinbox(root, from_=5, to=10)
sp1.grid(row=0, column=0)
sp2 = Spinbox(root, from_=5, to=10)
sp2.grid(row=1, column=0, columnspan=2, sticky="we",pady=2)
root.mainloop()
If you expand root window the ttk.Spinbox
spreads with it and an indentation occurs. I guess it is due to columnconfigure
but i need configuration for better look.
This appears to be a bug within Tk itself, and your Python code is simply exposing it. I converted your example code into straight Tcl/Tk and ran it against both Tk-8.5.17 and Tk-8.6.3 (via TclKits), and the same issue is happening there, too:
package require Tk 8.5
grid columnconfigure . 0 -weight 1
spinbox .spin -from 5 -to 10
grid .spin -row 0 -column 0
ttk::spinbox .spin2 -from 5 -to 10
grid .spin2 -row 1 -column 0 -sticky "ew" -pady 2
This is what the above code looks like when run:
It appears that the problem code is in the vistaTheme.tcl
file within the ttk
subdirectory, in the nested code beginning with ttk::style layout TSpinbox
:
ttk::style layout TSpinbox {
Spinbox.field -sticky nswe -children {
Spinbox.background -sticky news -children {
Spinbox.padding -sticky news -children {
Spinbox.innerbg -sticky news -children {
Spinbox.textarea -expand 1 -sticky {}
}
}
Spinbox.uparrow -side top -sticky ens
Spinbox.downarrow -side bottom -sticky ens
}
}
}
Specifically, if you remove the -sticky {}
bit from Spinbox.textarea -expand 1 -sticky {}
in the inner-most block, then it looks like the indentation goes away:
I'd suggest reading through the Tcl/Tk bug wiki here, then open a bug with them here for this issue. Wouldn't hold your breath, though. Tcl/Tk releases don't happen very often, because it's a pretty mature and stable language. If a fix is produced for Tcl/Tk, then you'll need to file a bug with the Python maintainers to get them to either update their internal copy of Tcl/Tk for the Windows releases or backport a fix.
It might be possible to work around the problem in Python by using ttk's styles and configuring the Spinbox.textarea
bit to unset the sticky attribute, however, I don't have an exact code snippet to do that at the moment.
这篇关于从 tkinter 创建新的 ttk 小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!