我的代碼應該執行以下操作:
單擊開始按鈕時開始將其大小增加一個像素
當達到視窗的大小時,它會回傳到其初始大小。真的做不到最后一部分
from tkinter import *
def increase():
global counter, x, y
x = 1
y = 1
b1.config(padx=x, pady=y)
b1.after(100, increase)
root=Tk()
x=5
y=2
# buttons
b1=Button(root, text="Start", bg="blue",
command=increase, width=x, height=y)
b1.place(x=10,y=10)
b2=Button(root, text="Stop", command=quit)
b2.place(x=10, y=260)
root.geometry("320x300")
root.resizable(False, False)
root.mainloop()
uj5u.com熱心網友回復:
像這樣的東西?
#!/usr/bin/python3
from tkinter import *
def increase():
global counter, x, y, root
x = 3
y = 3
window_height=root.winfo_height()
window_width=root.winfo_width()
if x >= window_height:
x=0
if y >= window_width:
y=0
b1.config(padx=x/3, pady=y/3)
b1.after(100, increase)
root=Tk()
x=5
y=2
# buttons
b1=Button(root, text="Start", bg="blue",
command=increase, width=x, height=y)
b1.place(x=10,y=10)
b2=Button(root, text="Stop", command=quit)
b2.place(x=10, y=260)
root.geometry("320x300")
root.resizable(False, False)
root.mainloop()
如果您使用寬度/高度或 padx/pady,按鈕大小會有所不同。我嘗試調整 padx/pady 的偏移量。
uj5u.com熱心網友回復:
您可以根據需要調整此代碼。這不是最好的方法,但它會完成作業。
from tkinter import *
root = Tk()
count = 0
def reset_count():
global count
count = 0
def btn_size_changer():
global count
if count <= 21:
if btn['width'] < 50 and btn['height'] < 40:
btn['width'] = 2
btn['height'] = 1
root.after(100, btn_size_changer)
count = 1
else:
if btn['width'] >= 7 and btn['height'] >= 3:
btn['width'] -= 2
btn['height'] -= 1
root.after(100, btn_size_changer)
else:
reset_count()
btn = Button(root, text="Click ME", command=lambda: btn_size_changer(), width=6, height=3)
btn.pack(anchor=CENTER)
root.geometry('350x350')
root.mainloop()
uj5u.com熱心網友回復:
放置小部件時使用 relx 和 trust 引數可能會很有趣。
relx 和 relx 可以是 0 到 1 之間的值。1 是視窗大小的 100%。0 是視窗大小的 0%。
您可以使用它來設定大小。如果達到視窗大小,則不需要計算。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/495420.html
上一篇:文本更改時的影片按鈕寬度
下一篇:我如何計算(>0)的型別?