2017-08-20 59 views
1

我終於完成了我一直在努力的一個骰子滾動程序,並且我想添加一件最後一件事:我想添加一個說「滾動骰子」的按鈕。有沒有辦法用Tkinter按鈕來運行Python腳本?我試過使用:如何使用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腳本,需要與多個骰子和切片成單獨的圖像一個圖像)

+0

通過pygame.Rect及其碰撞檢測方法可以很容易地在pygame中創建一個非常簡單的按鈕。你不需要tkinter。你必須執行另一個Python腳本也很奇怪。切片圖像是你可以用pygame做的事情,如果你想在另一個文件中做,那麼你應該導入切片圖像,而不是執行文件。 – skrx

+0

我已經用'pygame.Rect'發佈了一個解決方案,但是我會檢查一下tkinter是否也可以實現。但是,我不知道pygame和tkinter是如何協同工作的。 – skrx

+0

[這是一個答案](https://stackoverflow.com/a/16550818/6220679),告訴你如何將一個pygame顯示嵌入到tkinter.Frame中,但它有點奇怪,我寧願使用pygame的GUI庫(如[SGC](http://pygame.org/project-SGC-2089-4505.html))或者只是一個矩形。 – skrx

回答

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()