How do I use a Tkinter button to run a Python script? -


i finished dice roll program i've been working on , want add 1 last thing: want add button says "roll dice". there way run python script tkinter button? i've tried use:

from tkinter import *  master = tk()  def callback(): code here  b = button(master, text="button text", command=callback) b.pack()  mainloop() 

but when use that, pygame window black.

the code program is:

exec(open("dice crop.py").read(), globals()) pygame.locals import * random import randint 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()  1 = pygame.image.load("one.png").convert_alpha() 2 = pygame.image.load("two.png").convert_alpha() 3 = pygame.image.load("three.png").convert_alpha() 4 = pygame.image.load("four.png").convert_alpha() 5 = pygame.image.load("five.png").convert_alpha() 6 = pygame.image.load("six.png").convert_alpha()  counter = 0  while true:     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() 

(all exec(open("dice crop.py").read(), globals()) open python script takes 1 image multiple dice , slices separate images.)

you can use pygame.rect button. rects have collidepoint method can pass mouse position when user clicks mouse button. mouse events have pos attribute can pass collidepoint, or alternatively call 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)  1 = font.render("1", true, (0,200,0)) 2 = font.render("2", true, (0,200,0)) 3 = font.render("3", true, (0,200,0)) 4 = font.render("4", true, (0,200,0)) 5 = font.render("5", true, (0,200,0)) 6 = font.render("6", true, (0,200,0)) # put images list or dictionary # avoid repetition in 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:     evt in pygame.event.get():         if evt.type == pygame.quit:             pygame.quit()             sys.exit()         elif evt.type == pygame.mousebuttondown:             # check if mouse clicked on button.             if button.collidepoint(evt.pos):                 # start rolling , reset 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 current image (index [n-1] of list).     screen.blit(dice[n-1],(100,50))      pygame.time.delay(100)     pygame.display.update() 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -