简体   繁体   English

Tkinter:按下按钮后如何禁用它?

[英]Tkinter: How to disable a button after it is pressed?

So I am trying to make hangman in python using Tkinter and I have mostly finished the game but, I want to add a feature that disables the button once it is pressed so that the user doesn't guess the same letter twice.因此,我正在尝试使用 Tkinter 在 python 中制作刽子手,我已经完成了大部分游戏,但是我想添加一个功能,一旦按下按钮就会禁用按钮,这样用户就不会猜到同一个字母两次。 Any idea how I can implement it.知道如何实现它。

the code:https://drive.google.com/file/d/1v0tjlSZC_xHCQ0WopNPRJC1pLNaQ4wRR/view?usp=sharing代码:https://drive.google.com/file/d/1v0tjlSZC_xHCQ0WopNPRJC1pLNaQ4wRR/view?usp=sharing

Not only do you overwrite the button object when you create all the buttons, the button object will also be None because place() doesn't return anything.创建所有按钮时,不仅会覆盖button object,而且button object 也将为None因为place()不返回任何内容。 Split that line in two:将该行一分为二:

button = Button(...) and
button.place(...)

But even then the button object is overwritten for each new button, so the button object takes the value of the last button 'z'.但即便如此,每个新按钮都会覆盖按钮 object,因此按钮 object 采用最后一个按钮“z”的值。

To disable individual buttons, you need to be able to access each of them, eg by putting all objects in a list: button[n] = Button(...) Then in the guess() function you must disable exactly that button that was pressed.要禁用单个按钮,您需要能够访问每个按钮,例如通过将所有对象放入列表中: button[n] = Button(...)然后在guess() function 中,您必须完全禁用那个按钮被按下。

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

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