Python Forum
Want to dynamically update numbers using tkinter in pygame script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Want to dynamically update numbers using tkinter in pygame script
#1
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter - update/refresh treeview snakes 4 2,768 May-13-2021, 07:10 AM
Last Post: snakes
  [Tkinter] Tkinter - I have problem after import varaible or function from aGUI to script johnjh 2 1,037 Apr-17-2020, 08:12 PM
Last Post: johnjh
  Tkinter: increasing numbers and Radiobutton issue PeroPuri 1 769 Apr-13-2020, 05:48 PM
Last Post: deanhystad
  Unable to update or refresh label text in tkinter jenkins43 3 3,830 Jul-24-2019, 02:09 PM
Last Post: Friend
  Need help setting up a PySimpleGUI progress bar that I can maually update in script Soundtechscott 1 5,589 Jun-10-2019, 06:14 AM
Last Post: Soundtechscott
  [Tkinter] How can I use Tkinter with pygame microphone_head 4 7,372 May-13-2019, 10:34 AM
Last Post: kritesh
  Tkinter - Make changes to graph and update it adriancovaci 0 4,612 Apr-08-2019, 09:02 AM
Last Post: adriancovaci

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020