Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

python - How to create multiple, different custom scales in tkinter GUI?

I'm trying to create two custom scales in one GUI. The simplest way to create a custom scale is by using a style; but the problem is that when this function is called for the second time to create the second scale, the style.element_create() generates an error as it sees the 'custom.Horizontal.Scale.trough' as a duplicate.

def create_style():
    global startup
    if startup=="no":
        img_trough = PhotoImage(file="bar.gif")
        img_slider = PhotoImage(file="slider.gif")
    if startup=="yes":
        img_trough = PhotoImage(file="bar_small.gif")
        img_slider = PhotoImage(file="slider_small.gif")

   # create scale elements
   style.element_create('custom.Horizontal.Scale.trough', 'image', img_trough)
   style.element_create('custom.Horizontal.Scale.slider', 'image', img_slider)
   # create custom layout
   style.layout('custom.Horizontal.TScale',[('custom.Horizontal.Scale.trough', {'sticky': 'ns'}),
            ('custom.Horizontal.Scale.slider', {'side': 'left', 'sticky': '','children': [('custom.Horizontal.Scale.label', {'sticky': ''})]})])
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I managed to solve the problem. Leaving it for record to anyone who might need it. The root of the problem is the fact that "custom.Horizontal.Scale.trough" can not be used twice. Modifying it to "custom.Horizontal.Scale.trough2" or "custom.Horizontal.Scale2.trough" does not work. However, modifying the string "custom" can work. It is possible to use a different string everytime this function is called without affecting the functionality. This is what I did:

i=1
def create_style():
    global img_trough
    global img_slider
    global startup
    global i
    if startup=="no":
        img_trough = PhotoImage(file="bar.gif")
        img_slider = PhotoImage(file="slider.gif")

    if startup=="yes":
        img_trough = PhotoImage(file="bar_small.gif")
        img_slider = PhotoImage(file="slider_small.gif")


    print("called", i, "th time")
    i=i+1
    # create scale elements
    string_trough=str(i)+'.custom.Horizontal.Scale.trough'
    string_slider=str(i)+'.custom.Horizontal.Scale.slider'
    style.element_create(string_trough, 'image', img_trough)
    style.element_create(string_slider, 'image', img_slider)
    # create custom layout
    style.layout('custom.Horizontal.TScale',[(string_trough, {'sticky': 'ns'}),(string_slider, {'side': 'left', 'sticky': '','children': [('custom.Horizontal.Scale.label', {'sticky': ''})]})])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...