Python Forum

Full Version: Want to dynamically update numbers using tkinter in pygame script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, everyone.

I want to create a program that updates the number of times entered in real time after entering the controller.

This is the interface of the program.

[Image: 6AVwkzq]

I want to make the surrounding numbers increase when I press the corresponding button.

Here is the source code.

import pygame
from pygame.locals import *
import sys
import time
import tkinter
from tkinter import font

pygame.joystick.init()
pygame.init()

key_input = [0 for _ in range(9)]
label = []

past_time = time.time()

window = tkinter.Tk()
window.title("Contoller input")
window.geometry("640x360")

canvas = tkinter.Canvas(window, width = 640, height = 360)

canvas.create_rectangle(280, 195, 350, 330) #白鍵盤
canvas.create_rectangle(370, 195, 440, 330)
canvas.create_rectangle(460, 195, 530, 330)
canvas.create_rectangle(550, 195, 620, 330)

canvas.create_rectangle(325, 30, 395, 165, fill = "black") #黒鍵盤
canvas.create_rectangle(415, 30, 485, 165, fill = "black")
canvas.create_rectangle(505, 30, 575, 165, fill = "black")

canvas.create_oval(35, 65, 255, 285) #皿
canvas.create_line(145, 65, 145, 285)

canvas.place(x=0, y=0)

font1 = font.Font(family='Helvetica', size=10, weight='bold')

for i in range(9):
    label.append(tkinter.Label(window, text=key_input[i], font=font1))

label[0].place(x=310, y=333)
label[1].place(x=355, y=5)
label[2].place(x=400, y=333)
label[3].place(x=445, y=5)
label[4].place(x=490, y=333)
label[5].place(x=535, y=5)
label[6].place(x=580, y=333)
label[7].place(x=205, y=300)
label[8].place(x=100, y=300)

def replace(num):
    if(num == 0):
        label[num].place(x=310, y=333)
    elif(num == 1):
        label[num].place(x=355, y=5)
    elif(num == 2):
        label[num].place(x=400, y=333)
    elif(num == 3):
        label[num].place(x=445, y=5)
    elif(num == 4):
        label[num].place(x=490, y=333)
    elif(num == 5):
        label[num].place(x=535, y=5)
    elif(num == 6):
        label[num].place(x=580, y=333)
    elif(num == 7):
        label[num].place(x=205, y=300)
    elif(num == 8):
        label[num].place(x=100, y=300)
    

def pygame_loop():
    try:
        for e in pygame.event.get():
            if(e.type == pygame.locals.JOYBUTTONDOWN):
                print("press button " + str(e.button))
                key_input[e.button] += 1
                label[e.button] = tkinter.Label(window, text=key_input[e.button], font=font1)
                replace(e.button)
            if(e.type == pygame.locals.JOYAXISMOTION): #LR2 Type
                now_time = time.time()
                delta_time = now_time - past_time
                if(e.value < 0):
                    if(0.19 > delta_time or delta_time > 0.22):
                        print("Turntable spin to right")
                        key_input[7] += 1
                        label[7] = tkinter.Label(window, text=key_input[7], font=font1)
                        replace(7)
                if(e.value > 0):
                    key_input[8] += 1
                    print("Turntable spin to left")
                    label[8] = tkinter.Label(window, text=key_input[8], font=font1)
                    replace(8)
                past_time = now_time
    except KeyboardInterrupt:
        pygame.quit()
    window.after(1, pygame_loop)

pygame_loop()
window.mainloop()
My trouble is that the numbers are not updated when I enter the controller. Please tell me the improvements.