Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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': ''})]})])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...