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 5 20,560 Dec-02-2023, 07:05 PM
Last Post: aynous19
  PysimpleGUI window update dynamically SamLiu 6 3,889 Apr-05-2023, 02:32 PM
Last Post: SamLiu
  how to add two numbers and pass the result to the next page in tkinter? pymn 7 4,264 Feb-15-2022, 04:40 AM
Last Post: pymn
  Can't get tkinter database aware cascading comboboxes to update properly dford 6 3,536 Jan-11-2022, 08:37 PM
Last Post: deanhystad
  [Tkinter] Update variable using tkinter entry methon drSlump 6 5,100 Oct-15-2021, 08:01 AM
Last Post: drSlump
  [Tkinter] Tkinter - I have problem after import varaible or function from aGUI to script johnjh 2 2,524 Apr-17-2020, 08:12 PM
Last Post: johnjh
  Tkinter: increasing numbers and Radiobutton issue PeroPuri 1 2,121 Apr-13-2020, 05:48 PM
Last Post: deanhystad
  Unable to update or refresh label text in tkinter jenkins43 3 6,511 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 10,044 Jun-10-2019, 06:14 AM
Last Post: Soundtechscott
  [Tkinter] How can I use Tkinter with pygame microphone_head 4 14,690 May-13-2019, 10:34 AM
Last Post: kritesh

Forum Jump:

User Panel Messages

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