如何在tkinter GUI中创建多个不同的自定义比例? [英] How to create multiple, different custom scales in tkinter GUI?

查看:173
本文介绍了如何在tkinter GUI中创建多个不同的自定义比例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一个GUI中创建两个自定义比例.创建自定义比例的最简单方法是使用样式.但是问题在于,当第二次调用此函数以创建第二个比例尺时,style.element_create()会生成一个错误,因为它将"custom.Horizo​​ntal.Scale.trough"视为重复项.

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

推荐答案

我设法解决了这个问题.将其留给可能需要它的任何人记录. 问题的根源在于"custom.Horizo​​ntal.Scale.trough"不能两次使用.将其修改为"custom.Horizo​​ntal.Scale.trough2"或"custom.Horizo​​ntal.Scale2.trough"不起作用.但是,修改字符串"custom"可以起作用.每次调用此函数时都可以使用其他字符串,而不会影响功能.这就是我所做的:

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

这篇关于如何在tkinter GUI中创建多个不同的自定义比例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆