简体   繁体   English

为什么我的滑块在python tkinter中消失了?

[英]why do my sliders disappear in python tkinter?

I've been working on a tkinter project that uses three sliders to customize an RGB output. 我一直在研究一个使用三个滑块来定制RGB输出的tkinter项目。 The code bellow works just fine: 下面的代码工作得很好:

from tkinter import*


Window = Tk()
Window.title("Edit Color")
Window.geometry('1270x680')
Window.configure(background = "black")

#sliders
Slider_1 = Scale(activebackground='pink',
                 orient=HORIZONTAL,
                 relief=GROOVE,
                 length = 500,
                 from_=0,to_=255,
                 bg='red',
                 tickinterval=51,
                 troughcolor='pink')
Slider_1.grid(row=0, column=0,padx=60, pady=70)


Slider_2 = Scale(activebackground='lightGreen',
                 orient=HORIZONTAL,
                 relief=GROOVE,
                 length = 500,
                 from_=0,to_=255,
                 bg='Green',
                 tickinterval=51,
                 troughcolor='lightGreen')
Slider_2.grid(row=2,  column=0, pady=70)


Slider_3 = Scale(activebackground= 'light blue',
                 orient=HORIZONTAL,relief=GROOVE,
                 length = 500, from_=0, to_=255,
                 bg='blue',
                 tickinterval=51,
                 troughcolor= 'light blue')
Slider_3.grid(row=4, column=0, pady=70)


Window.mainloop()

Then I added two things in order to be able to print the values of the sliders. 然后我添加了两个东西,以便能够打印滑块的值。 The first is near the top. 第一个是靠近顶部。 It is a function which is associated with a parameter called ”command”, that can be found at the end of the parentheses of each slider function: 它是一个与名为“command”的参数相关联的函数,可以在每个滑块函数的括号末尾找到:

from tkinter import*


Window = Tk()
Window.title("Edit Color")
Window.geometry('1270x680')
Window.configure(background = "black")

def print_value(val):
    print val

#sliders
    Slider_1 = Scale(activebackground='pink',
                 orient=HORIZONTAL,
                 relief=GROOVE,
                 length = 500,
                 from_=0,to_=255,
                 bg='red',
                 tickinterval=51,
                 troughcolor='pink',command=print_value)
    Slider_1.grid(row=0, column=0,padx=60, pady=70)


    Slider_2 = Scale(activebackground='lightGreen',
                 orient=HORIZONTAL,
                 relief=GROOVE,
                 length = 500,
                 from_=0,to_=255,
                 bg='Green',
                 tickinterval=51,
                 troughcolor='lightGreen',command=print_value)
    Slider_2.grid(row=2,  column=0, pady=70)


    Slider_3 = Scale(activebackground= 'light blue',
                 orient=HORIZONTAL,relief=GROOVE,
                 length = 500, from_=0, to_=255,
                 bg='blue',
                 tickinterval=51,
                 troughcolor= 'light blue',command=print_value)
    Slider_3.grid(row=4, column=0, pady=70)


Window.mainloop()


When I added this and executed the program the screen was black and I had no Sliders. 当我添加这个并执行程序时,屏幕是黑色的,我没有滑块。

what do I do? 我该怎么办?

创建滑块的代码位于函数print_value ,您永远不会调用print_value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM