如何使用Tkinter按钮运行Python脚本?

时间:2017-08-20 04:40:16

标签: python button tkinter python-3.5

我终于完成了一个我一直在努力的骰子滚动程序,我想添加最后一件事:我想添加一个“Roll Dice”按钮。有没有办法用Tkinter按钮运行Python脚本?我试过用:

from tkinter import *

master = Tk()

def callback():
CODE HERE

b = Button(master, text="BUTTON TEXT", command=callback)
b.pack()

mainloop()

但是当我使用它时,pygame窗口只是黑色。

我的程序代码是:

exec(open("Dice Crop.py").read(), globals())
from pygame.locals import *
from random import randint
from tkinter import *
import pygame
import sys

pygame.init()

font = pygame.font.SysFont("comicsansms",30)

screen = pygame.display.set_mode((284,177),0,32)

background = pygame.image.load("background.jpg").convert()

one = pygame.image.load("one.png").convert_alpha()
two = pygame.image.load("two.png").convert_alpha()
three = pygame.image.load("three.png").convert_alpha()
four = pygame.image.load("four.png").convert_alpha()
five = pygame.image.load("five.png").convert_alpha()
six = pygame.image.load("six.png").convert_alpha()

counter = 0

while True:
    for evt in pygame.event.get():
        if evt.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.blit(background,(0, 0))

    if counter < 20:
        n = randint(1,6)
        counter += 1

    if n == 1:
        screen.blit(one,(100,50))
        screen.blit(font.render("1",True,(0,200,0)),(125,130))
    if n == 2:
        screen.blit(two,(100,50))
        screen.blit(font.render("2",True,(0,200,0)),(125,130))
    if n == 3:
        screen.blit(three,(100,50))
        screen.blit(font.render("3",True,(0,200,0)),(125,130))
    if n == 4:
        screen.blit(four,(100,50))
        screen.blit(font.render("4",True,(0,200,0)),(125,130))
    if n == 5:
        screen.blit(five,(100,50))
        screen.blit(font.render("5",True,(0,200,0)),(125,130))
    if n == 6:
        screen.blit(six,(100,50))
        screen.blit(font.render("6",True,(0,200,0)),(125,130))
    if counter < 20:
        print(n)
    if counter == 20:
        print(">",n,"<")


    pygame.time.delay(100)
    pygame.display.update()

(所有exec(open("Dice Crop.py").read(), globals())所做的是打开一个Python脚本,该脚本将一个图像与多个骰子一起切成单独的图像。)

1 个答案:

答案 0 :(得分:0)

您可以使用pygame.Rect作为按钮。 Rects有一个collidepoint方法,当用户单击鼠标按钮时,您可以将鼠标移动到该方法。鼠标事件具有pos属性,您可以将其传递给collidepoint,或者调用pygame.mouse.get_pos()

from random import randint
import pygame
import sys

pygame.init()

font = pygame.font.SysFont("comicsansms", 30)
screen = pygame.display.set_mode((284, 177), 0, 32)

one = font.render("1", True, (0,200,0))
two = font.render("2", True, (0,200,0))
three = font.render("3", True, (0,200,0))
four = font.render("4", True, (0,200,0))
five = font.render("5", True, (0,200,0))
six = font.render("6", True, (0,200,0))
# Put the images into a list or dictionary to
# avoid the repetition in the while loop.
dice = [one, two, three, four, five, six]

button = pygame.Rect(5, 5, 120, 40)
button_text = font.render("roll dice", True, (0,200,0))
rolling = False
counter = 0
n = 0

while True:
    for evt in pygame.event.get():
        if evt.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif evt.type == pygame.MOUSEBUTTONDOWN:
            # Check if the mouse clicked on the button.
            if button.collidepoint(evt.pos):
                # Start rolling and reset the counter.
                rolling = True
                counter = 0

    if rolling:
        if counter < 20:
            n = randint(1,6)
            counter += 1
            print(n)
        elif counter == 20:
            rolling = False
            print(">",n,"<")

    screen.fill((30, 30, 30))
    pygame.draw.rect(screen, (70, 80, 90), button)
    screen.blit(button_text, (button.x+3, button.y-2))
    # Blit the current image (index [n-1] of the list).
    screen.blit(dice[n-1],(100,50))

    pygame.time.delay(100)
    pygame.display.update()