Tkinter的按钮动画 [英] Tkinter button animation
问题描述
所以我一直在使用Tkinter的一段简单的Python 3.4 code的。这是D&放一个骰子滚筒; d。它有所有的基本骰子按钮,以及一个允许你输入一个边数。我把它的方式让我想起了键盘,所以我想我应该在的东西,可以让你通过推动按钮,点击按钮添加。我想通了系统点击按钮来获得要调用的函数,但我无法弄清楚如何使按钮动画去了。有没有办法做到这一点?请帮忙。谢谢!
从Tkinter的进口*
进口tkinter.simpledialog作为simpledialog
从随机进口randint
进口OS 根= TK()
root.title(死滚筒)
root.geometry(200x215)
应用=帧(根)
app.grid() V = STRINGVAR()
W =标签(根,textvariable = V)
w.grid() 打印(结果)
高清AD4():
X = randint(1,4)
v.set(X)
打印(X) 高清AD6():
X = randint(1,6)
v.set(X)
打印(X) 高清AD8():
X = randint(1,8)
v.set(X)
打印(X) 高清AD10():
X = randint(1,10)
v.set(X)
打印(X) 高清AD12():
X = randint(1,12)
v.set(X)
打印(X) 高清AD20():
X = randint(1,20)
v.set(X)
打印(X) 高清AD100():
X = randint(1,100)
v.set(X)
打印(X) DEF其他():
尝试:
X = simpledialog.askinteger(两岸,有多少双方都没有?)
Y = randint(1,X)
打印(Y)
v.set(Y)
除了类型错误:
CLS() 高清CLS():
使用os.system(['清','CLS'] [os.name =='NT']) D4 =按钮(应用程序,文本=D4,命令= AD4,身高= 2,宽度= 5,FG =白,BG =蓝)
d4.grid(行= 0,列= 0,padx = 10,pady = 10) D6 =按钮(应用程序,文本=D6,命令= AD6,身高= 2,宽度= 5,FG =白,BG =蓝)
d6.grid(行= 0,列= 1,padx = 10,pady = 10) D8 =按钮(应用程序,文本=D8,命令= AD8,身高= 2,宽度= 5,FG =白,BG =蓝)
d8.grid(行= 0,列= 2,padx = 10,pady = 10) D10 =按钮(应用程序,文本=D10,命令= AD10,身高= 2,宽度= 5,FG =白,BG =蓝)
d10.grid(行= 1,列= 0,padx = 10,pady = 10) D12 =按钮(应用程序,文本=D12,命令= AD12,身高= 2,宽度= 5,FG =白,BG =蓝)
d12.grid(行= 1,列= 1,padx = 10,pady = 10) D20 =按钮(应用程序,文本=D20,命令= AD20,身高= 2,宽度= 5,FG =白,BG =蓝)
d20.grid(行= 1,列= 2,padx = 10,pady = 10) D100 =按钮(应用程序,文本=D100,命令= AD100,身高= 2,宽度= 5,FG =白,BG =蓝)
d100.grid(行= 2,列= 0,padx = 10,pady = 10) otherbutton =按钮(应用程序,文本=其他,命令=其他,身高= 2,宽度= 5,FG =白,BG =蓝)
otherbutton.grid(行= 2,列= 1,padx = 10,pady = 10) clearButton =按钮(应用程序,文本=清除,命令= CLS,身高= 2,宽度= 5,FG =白,BG =蓝)
clearButton.grid(行= 2,列= 2,padx = 10,pady = 10) DEF安其preSS(事件):
如果event.char =='7':
AD4()
如果event.char =='8':
AD6()
如果event.char =='9':
AD8()
如果event.char =='4':
AD10()
如果event.char =='5':
AD12()
如果event.char =='6':
AD20()
如果event.char =='1':
AD100()
如果event.char =='2':
其他()
如果event.char =='3':
CLS()
root.bind('<关键preSS>',安其preSS) root.mainloop()
下面是一个如何使按钮看起来就像你pressed它,当你打它的关键一个简短的演示。诀窍是它的救援
参数更改为凹陷,稍等片刻,然后更改救援
回提高。
#!的/ usr /斌/包膜蟒蛇动画一个Tkinter的按钮 使按钮看起来像它已经pressed当用户
点击与该按钮相关联的键。 见http://stackoverflow.com/q/31900552/4014959 撰稿PM 2Ring 2015年8月9日
导入Tkinter的传统知识MAXBUTTON = 5
maxkey = STR(MAXBUTTON)高清button_cb(CH):
打印(按钮+ CH +'pressed')DEF安其preSS(事件):
CH = event.char
如果'1'< = CH< = maxkey:
从字典#Retrieve此按钮
B =按钮[CH] #Simulate按压按钮
b.config(浮雕= tk.SUNKEN)
button_cb(CH) #Let它弹回后200毫秒
b.after(200,拉姆达:b.config(浮雕= tk.RAISED))根= tk.Tk()
root.title(键/按钮演示)#A快译通保存按钮。字典的键按钮文本。
按钮= {}了#make一些按钮
对于i在范围(1,MAXBUTTON + 1):
S = STR(I)
B = tk.Button(根,文本= S,命令=拉姆达CH = S:button_cb(CH))
b.pack(侧= tk.LEFT) 在字典#Save此按钮
按钮[S] = Broot.bind('<关键preSS>',安其preSS)root.mainloop()
如果你愿意,你也可以使按钮点亮暂时(当你将鼠标悬停在它喜欢它),当你preSS关键。要做到这一点,你需要主动和作者之间切换按钮的状态
。结果
例如,改变结果 b.config(浮雕= tk.SUNKEN)
结果
到 b.config(浮雕= tk.SUNKEN,状态= tk.ACTIVE)
和改变结果 b.after(200,拉姆达:b.config(浮雕= tk.RAISED))
结果
到 b.after(200,拉姆达:b.config(浮雕= tk.RAISED,状态= tk.NORMAL))
So I have have been working on a simple piece of python 3.4 code using tkinter. It is a dice roller for D&D. it has buttons for all the basic dice, and one that allows you to input a number of sides. The way I laid it out reminded me of a keypad, so i thought I should add in something that allows you to click the buttons by pushing the buttons. I figured out the system for clicking the buttons to get the function to call, but I can't figure out how to make the button animation go. Is there a way to do this? Please help. Thanks!
from tkinter import *
import tkinter.simpledialog as simpledialog
from random import randint
import os
root = Tk()
root.title("Die Roller")
root.geometry("200x215")
app = Frame(root)
app.grid()
v = StringVar()
w = Label(root, textvariable= v)
w.grid()
print("Results:")
def ad4():
x = randint(1,4)
v.set(x)
print(x)
def ad6():
x = randint(1,6)
v.set(x)
print(x)
def ad8():
x = randint(1,8)
v.set(x)
print(x)
def ad10():
x = randint(1,10)
v.set(x)
print(x)
def ad12():
x = randint(1,12)
v.set(x)
print(x)
def ad20():
x = randint(1,20)
v.set(x)
print(x)
def ad100():
x = randint(1,100)
v.set(x)
print(x)
def other():
try:
x = simpledialog.askinteger("Sides", "How many sides are there?")
y = randint(1, x)
print(y)
v.set(y)
except TypeError:
cls()
def cls():
os.system(['clear','cls'][os.name == 'nt'])
d4 = Button(app, text = "d4", command=ad4, height = 2, width = 5, fg="white", bg="blue")
d4.grid(row=0, column=0, padx=10, pady=10)
d6 = Button(app, text = "d6", command=ad6, height = 2, width = 5, fg="white", bg="blue")
d6.grid(row=0, column=1, padx=10, pady=10)
d8 = Button(app, text = "d8", command=ad8, height = 2, width = 5, fg="white", bg="blue")
d8.grid(row=0, column=2, padx=10, pady=10)
d10 = Button(app, text = "d10", command=ad10, height = 2, width = 5, fg="white", bg="blue")
d10.grid(row=1, column=0, padx=10, pady=10)
d12 = Button(app, text = "d12", command=ad12, height = 2, width = 5, fg="white", bg="blue")
d12.grid(row=1, column=1, padx=10, pady=10)
d20 = Button(app, text = "d20", command=ad20, height = 2, width = 5, fg="white", bg="blue")
d20.grid(row=1, column=2, padx=10, pady=10)
d100 = Button(app, text = "d100", command=ad100, height = 2, width = 5, fg="white", bg="blue")
d100.grid(row=2, column=0, padx=10, pady=10)
otherbutton = Button(app, text = "Other", command=other, height = 2, width = 5, fg="white", bg="blue")
otherbutton.grid(row=2, column=1, padx=10, pady=10)
clearButton = Button(app, text = "Clear", command=cls, height =2, width = 5, fg="white", bg="blue")
clearButton.grid(row=2, column=2, padx=10, pady=10)
def onKeyPress(event):
if event.char == '7':
ad4()
if event.char == '8':
ad6()
if event.char == '9':
ad8()
if event.char == '4':
ad10()
if event.char == '5':
ad12()
if event.char == '6':
ad20()
if event.char == '1':
ad100()
if event.char == '2':
other()
if event.char == '3':
cls()
root.bind('<KeyPress>', onKeyPress)
root.mainloop()
Here's a short demo of how to make the button look like you've pressed it when you hit its key. The trick is to change its relief
parameter to SUNKEN, wait a moment, and then change the relief
back to RAISED.
#!/usr/bin/env python
""" "Animate" a Tkinter button
Make the button look like it's been pressed when the user
hits the key associated with the button.
See http://stackoverflow.com/q/31900552/4014959
Written by PM 2Ring 2015.08.09
"""
import tkinter as tk
maxbutton = 5
maxkey = str(maxbutton)
def button_cb(ch):
print('Button ' + ch + ' pressed')
def onKeyPress(event):
ch = event.char
if '1' <= ch <= maxkey:
#Retrieve this button from the dict
b = buttons[ch]
#Simulate pushing the button
b.config(relief=tk.SUNKEN)
button_cb(ch)
#Let it pop back up after 200 milliseconds
b.after(200, lambda: b.config(relief=tk.RAISED))
root = tk.Tk()
root.title("Key / Button Demo")
#A dict to save the buttons in. The dict keys are the button texts.
buttons = {}
#Make some buttons
for i in range(1, maxbutton + 1):
s = str(i)
b = tk.Button(root, text=s, command=lambda ch=s: button_cb(ch))
b.pack(side=tk.LEFT)
#Save this button in the dict
buttons[s] = b
root.bind('<KeyPress>', onKeyPress)
root.mainloop()
If you like, you can also make the button light up temporarily (like it does when you hover the mouse over it) when you press its key. To do that, you need to toggle the button's state
between ACTIVE and NORMAL.
Eg, change
b.config(relief=tk.SUNKEN)
to
b.config(relief=tk.SUNKEN, state=tk.ACTIVE)
and change
b.after(200, lambda: b.config(relief=tk.RAISED))
to
b.after(200, lambda: b.config(relief=tk.RAISED, state=tk.NORMAL))
这篇关于Tkinter的按钮动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!