+ 1
Can someone complete this code by adding a button to it? Im not familiar with tk or pygame.
import pygame from tkinter import * pygame.init() window = pygame.display.set_mode((720,454)) pygame.display.set_caption("Window") black = (0,0,0) image=pygame.image.load("C:\Python32\Image224.png").convert_alpha() gameLoop=True while gameLoop: for event in pygame.event.get(): if (event.type==pygame.QUIT): gameLoop=False window.fill(black) window.blit(image, (0,0)) pygame.display.flip() root = Tk()
12 Respuestas
+ 3
Put it under the root = Tk()
You will have to mess around with it because I’m not familiar with the pygame stuff you are doing but that’s how you make a button with tkinter
+ 3
The YouTube channel sentdex has a good tkinter playlist for python. I would watch it from the beginning so you get a better foundation.
+ 3
Just make sure it’s for the version of python because tkinter is a little different in python 2 than 3.
+ 3
The b is what we named the new instance of the button object and .pack() is the member function that packs it into the tk window. So that should work. You know the command=yourFunction part I meant you would put the name of the function you want to call when you press the button not actually put YourFunction right?
+ 3
The .pack() will just pack the button wherever the window has space. You can use .grid() http://effbot.org/tkinterbook/grid.htm that will help you with that. Coloring the button is easy too if you are using Mac tho it won’t work. Use that site and let me know if you get stuck.
0
b = Button(root, text=“push me”, command=yourFunction)
b.pack()
0
Where would I put that code? Does it matter?
0
Do you know of a good tutorial that explains how to use this line of code? I’m close to clueless
0
I tried doing what you suggested but it is saying the ‘b’ in b.pack is an invalid syntax. Any ideas?
0
Ok thanks!! I do now!!🤣🤣. So how do I color the button and position the buttons (x,y)
0
Ok!! Thank you so much!!!!
0
Ok Arthur so I did what you said sent FRC helped a lot but I’ve now ran into a problem with putting text on the buttons. It says the font he used isn’t readable. Here is the code if you want to look over it. import pygame
from tkinter import *
def example():
print("Hi")
z = 3+9
print(z)
red =(200,0,0)
light_red=(255,0,0)
blue =(2,57,255)
light_blue=(2,128,255)
black = (0,0,0)
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
pygame.init()
window = pygame.display.set_mode((700,700))
pygame.display.set_caption("Window")
image=pygame.image.load("C:\Python32\Intro.png").convert_alpha()
gameLoop=True
while gameLoop:
for event in pygame.event.get():
if(event.type==pygame.QUIT):
gameLoop=FALSE
quit()
mouse = pygame.mouse.get_pos()
#print(mouse)
if 125+100 > mouse[0] > 150 and 450+50 > mouse[1] > 450:
pygame.draw.rect(window, light_blue,(125,450,100,50))
else:
pygame.draw.rect(window, blue,(125,450,100,50))
smallText = pygame.font.Font("freesandsbold.ttf", 20)
textSurf, textRect = text_objects("Start!", smallText)
textRect.center = ( (125+(100/2)), (450+(50/2)) )
window.blit(textSurf, textRect)
pygame.draw.rect(window, red,(465,450,100,50))
window.blit(image, (100,0))
pygame.display.flip()
root = Tk()