Tkinter 按钮动画

tat*_*obs 3 python tkinter button dice

我一直在使用 tkinter 编写一段简单的 python 3.4 代码。它是 D&D 的骰子滚轮。它有用于所有基本骰子的按钮,还有一个允许您输入骰子面数的按钮。

我的布局方式让我想起了键盘,所以我想我应该添加一些东西,让你可以通过按下按钮来单击按钮。我弄清楚了单击按钮以调用函数的系统,但我不知道如何使按钮动画运行。

有没有办法做到这一点?请帮忙。谢谢!

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()
Run Code Online (Sandbox Code Playgroud)

PM *_*ing 6

下面是一个简短的演示,演示了如何使按钮在您按下按键时看起来就像您按下了它一样。诀窍是将其relief参数更改为 SUNKEN,稍等片刻,然后将其更改relief回 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()
Run Code Online (Sandbox Code Playgroud)

如果您愿意,您还可以在按下按键时使按钮暂时亮起(就像将鼠标悬停在其上时一样)。为此,您需要state在“活动”和“正常”之间切换按钮。
例如,更改
b.config(relief=tk.SUNKEN)

b.config(relief=tk.SUNKEN, state=tk.ACTIVE)

并更改
b.after(200, lambda: b.config(relief=tk.RAISED))

b.after(200, lambda: b.config(relief=tk.RAISED, state=tk.NORMAL))