Create new ttk widget from tkinter

青春壹個敷衍的年華 提交于 2019-12-05 08:52:21

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.

Using python 3.4 on windows 7 I don't get the same indentation. Here is a demo:

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()
    opts = { 'from': 0, 'to': 10, 'increment': 1 }
    sp1 = tk.Spinbox(root, **opts)
    sp1.place(x=5, y=5)
    sp2 = Spinbox(root, **opts)
    sp2.place(x=5, y=30)
    root.mainloop()

This yields the following:

If you are getting an extra indent in the entry field area perhaps you are formatting the value with spaces or tabs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!