简体   繁体   English

来自一键 TKINTER 的多个事件

[英]Multiple Events From one Button TKINTER

I realize some people may have already asked this question but i'd like to task it in my way in order to get a specific response.我意识到有些人可能已经问过这个问题,但我想以我的方式来完成任务以获得具体的答复。

So im using Tkiner in pyhton 2.7.所以我在pyhton 2.7中使用Tkiner。

Basically I want a single button which is displayed by default with the initial text value of First Click and a Label which says Blah by default.基本上我想要一个默认显示的单个按钮,初始文本值为 First Click 和一个默认显示 Blah 的标签。 When "First Click" has been clicked it should change the text value inside that button to "Second Click" and also change the text value inside the Label to BlahBlah.单击“第一次单击”后,应将该按钮内的文本值更改为“第二次单击”,并将标签内的文本值更改为 BlahBlah。 And once again when "Second Click" has been clicked it should again change the text value of "second Click" to be "Third Click" and also again change the value of the Label to be "BlahBlahBlah" and now clicking the "Third Button" should change nothing at all and everything should remain as is.再次单击“第二次单击”时,应再次将“第二次单击”的文本值更改为“第三次单击”,并将标签的值再次更改为“BlahBlahBlah”,然后单击“第三次单击” “应该什么都不改变,一切都应该保持原样。

Please note that i'm a newbie and I would prefer the easiest(dumbest) level possible so I can start learning from somewhere.请注意,我是一个新手,我更喜欢最简单(最笨)的级别,这样我就可以从某个地方开始学习。 Basically something that would make sense to a dummy.基本上是对假人有意义的东西。

Iv'e done a lot of research and have failed to understand how to do it and have resorted to asking my specific question to everyone that is willing to help.我做了很多研究,但未能理解如何去做,并求助于向愿意提供帮助的每个人提出我的具体问题。

Cheers.干杯。

You could just create a counter to keep track of these clicks and use conditions to change the text accordingly.您可以创建一个计数器来跟踪这些点击并使用条件相应地更改文本。

import tkinter as tk

def switch():

     if root.counter <= 1:
          if not root.counter:
               a_label['text'] = 'Second Click'
               a_button['text'] = 'Second Button'
          else:
               a_label['text'] = 'Third Click'
               a_button['text'] = 'Third Button'
          root.counter += 1

root = tk.Tk()
root.counter = 0
a_label = tk.Label(root, text='Blah')
a_label.pack()
a_button = tk.Button(root, text='First Click', command=switch)
a_button.pack()
root.mainloop()

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

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