Python 布尔值不能正常工作

Python 布尔值不能正常工作,python,tkinter,boolean,Python,Tkinter,Boolean,我试图得到下面的代码来创建两个按钮,当你按下一个按钮时会显示音量控制器,当按下另一个按钮时会隐藏音量控制器,但很明显,这是行不通的,我想这主要是因为我不知道布尔值在python中是如何工作的,所以如果有人能帮助我,我将不胜感激 from tkinter import * #first window master= Tk() master.geometry('1440x900+0+0') master.title('DMX512 Controller') #buttons b

我试图得到下面的代码来创建两个按钮,当你按下一个按钮时会显示音量控制器,当按下另一个按钮时会隐藏音量控制器,但很明显,这是行不通的,我想这主要是因为我不知道布尔值在python中是如何工作的,所以如果有人能帮助我,我将不胜感激

from tkinter import *

#first window   
master= Tk()
master.geometry('1440x900+0+0')    
master.title('DMX512 Controller')



#buttons
bw=250
bh=110

bool1show = False



Button(master,text="show the slider", command =bool1show= True).place(x=800,y=10)
Button(master,text="hide the slider", command = bool1show= not True).place(x=900,y=10)




#slider characteristics
slw=130
sll=600
sly=1
stc='blue'


if bool1show==True:
    Scale(master, from_=255, to=0, length =sll,width =slw, troughcolor = stc).grid(row=sly,column=5)
if bool1show==not True:
    Scale(from_=255, to=0, length =sll,width =slw, troughcolor = stc).grid(row=sly,column=5)

你的代码中有一些模棱两可的地方,所以我不确定你想达到什么目的。如果要验证真实情况,可以执行以下操作:

if bool1show == True:
  do stuff...
#or
if bool1show:
  do stuff...
if bool1show==(not True)
#or better:
if bool1show == False
#even better:
if not bool1show:
如果bool1show==not True:不起作用。如果你真的想这样做,你可以:

if bool1show == True:
  do stuff...
#or
if bool1show:
  do stuff...
if bool1show==(not True)
#or better:
if bool1show == False
#even better:
if not bool1show:

希望这有助于更好地理解python布尔函数

如果将
命令
参数指定给调用另一个函数的lambda函数,会怎么样:

Button(master,text="show the slider", command=lambda: bool_func(bool1show).place(x=800,y=10)
Button(master,text="hide the slider",command=lambda: bool_func(bool1show,b=False).place(x=900,y=10)
其中:

def bool_func(variable, b=True): # b is True by default, hence you don't provide the argument in the first Button statement.
    variable = b
    return variable

有关lambda函数的一些信息,请查看

您必须在
命令
参数中引用函数,并且
bool1show=True
不是对函数的引用。然而,由于您所做的只是显示或隐藏一个小部件,所以您根本不需要使用布尔变量,除非您使用的是单选按钮(而不是单选按钮)

对于您发布的代码,您只需要两个函数:一个用于显示滑块,另一个用于隐藏滑块。为此,只需创建一次比例,然后使用
栅格
方法显示和隐藏它。要使其工作,您必须通过在全局变量中保存引用来“记住”scale小部件

def show_scale():
    # note: grid will remember the values you used when you first called grid
    the_scale.grid()

def hide_scale():
    # this removes the scale from view, but remembers where it was
    the_scale.grid_remove()

the_scale = Scale(master, from_=255, to=0, length =sll,width =slw, troughcolor = stc)
the_scale.grid(row=sly, column=5)
...
Button(master,text="show the slider", command show_scale).place(x=800,y=10)
Button(master,text="hide the slider", command hide_scale).place(x=900,y=10)

另一方面,我强烈建议你们不要使用这个地方。您的GUI将更易于编写和维护,并且在调整大小或在具有不同分辨率或不同字体的系统上运行时,它们的性能会更好

如果bool1show==not True则无效。如果不是bool1show==True,请尝试
;如果不是bool1show,请尝试
@karthikr:这完全有效
not True
False
,因此您只是将
BoolShow
False
进行比较,如果不是BoolShow
(假设
BoolShow
是一个布尔值),这是一种有效(但笨拙)的编写
的方法
bool1show==not True
无效。啊,在
not True
周围需要括号。从逻辑上讲,它是有效的。这是你的全部代码吗?我认为tkinter应用程序需要一个
mainloop
调用。顺便说一句,在
mainloop
之前出现的任何代码执行速度都非常快,用户在此之前不可能单击任何控件。因此,
bool1show
在它到达你的条件时肯定是假的。我基本上是想说,bool1show开始时为假,如果按下这个按钮,则打开bool1show true如果按下另一个按钮,则打开boolshow1 false,如果bool1show等于true…显示滑块,如果bool1show等于false,请不要显示滑块…………您能给我一个示例,说明如何使其工作吗?谢谢
command=True
不会做你认为它能做的事(除非你认为它什么都没做…。@BryanOakley-是的,你是对的。我不熟悉tinkter库,但在阅读了一些文档后,我理解命令应该是一个方法或函数。我删掉了那部分。Thanks@user2996828也许你应该重新提出你的问题。我的答案与python中的布尔值是如何工作的有关。要回答你的新问题,我需要熟悉tkinter软件包,而我不是…谢谢,这很完美,比我尝试的方式更有意义!我不知道网格删除命令!!!再次感谢!