【问题标题】:How to increase decrease a button for a while in tkinter?如何在tkinter中增加减少按钮一段时间?
【发布时间】:2021-06-28 07:09:31
【问题描述】:

我是新来的,并且是 Python 3.x 的初学者

我正在尝试在 tkinter 中创建一个 Button 或两个,例如,当我单击第一个按钮时,第三个或第四个按钮将增加和减少(使用宽度 16 和 18)10 次,然后再增加 10 次恢复为普通按钮。不使用class

代码:

from tkinter import *
import time
window1 = Tk()
window1.geometry("300x300")
btn1=Button(text="16",font=40,width=16)
btn1.pack()

# here I want to create a loop to flash the button 10 times
# I don't know which way is better :
# to use time.sleep with for in range or use .after
# or use def with loops
#can I put line 14 and 13 together ?!
#why the first button in line 5 does not appear instantly !? why line17 affect 
that so I put #?
btn1["width"]=18
btn1["text"]="18"
btn1.pack()
#time.sleep(1)
btn1["width"]=16
btn1["text"]="16"
btn1.pack()
window1.mainloop()

【问题讨论】:

  • 欢迎来到 SO。如果您向我们展示您到目前为止所做的事情会更好。
  • 稍后使用config() 方法更新小部件的属性。

标签: tkinter button


【解决方案1】:

这里用这个:

from tkinter import *


def change(event=None):

    global count
    
    btn2['width']= 18
    btn2.after(500, lambda: btn2.config(width=16))

    if count< 10:
        btn2.after(1000, change)
        count += 1 
        
    else:
        count=0
        
window1 = Tk()
window1.geometry("300x300")

count = 0

btn1=Button(text="btn1",font=40,width=16)
btn1.pack()
btn1.bind('<Button>', change)


btn2=Button(text="btn2",font=40,width=16)
btn2.pack()

window1.mainloop()

【讨论】:

  • 最好有一些解释。
  • @MoneymBrussel 进行了编辑。告诉我这是否是您要查找的内容,我将添加解释。
  • 谢谢你,这就是我正在寻找的,现在我将开始进行一些我想要的修改,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-12
  • 2021-11-29
  • 2020-01-19
  • 1970-01-01
  • 2023-02-02
  • 1970-01-01
相关资源
最近更新 更多