gpt4 book ai didi

python - Raspberry Pi、tkinter、触发器上的屏幕计数器、图像问题

转载 作者:太空宇宙 更新时间:2023-11-03 15:10:25 25 4
gpt4 key购买 nike

我有一个简单的脚本,它在屏幕底部显示一个终止按钮,并在触发 GPIO 时显示一个计数器。我希望计数器显示在屏幕中间。但是,它实际上向上推了“kill”按钮并在其下方显示计数器。如何修复此问题以在屏幕中间显示我的计数器?

启动屏幕

GPIO后的屏幕

from time import sleep  # Allows us to call the sleep function to slow down our loop
import RPi.GPIO as GPIO # Allows us to call our GPIO pins and names it just GPIO
import tkinter as tk
from tkinter import *

GPIO.setmode(GPIO.BCM) # Set's GPIO pins to BCM GPIO numbering
BUTTON_1 = 23 # Sets our input pins
BUTTON_2 = 24 # Sets our input pins
BUTTON_3 = 25 # Sets our input pins
GPIO.setup(BUTTON_1, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set our input pin to be an input, with internal pullup resistor on
GPIO.setup(BUTTON_2, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set our input pin to be an input, with internal pullup resistor on
GPIO.setup(BUTTON_3, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set our input pin to be an input, with internal pullup resistor on

counter = 0
def counter_label(label):
def count():
global counter
counter += 1
#m = Label(text=str(counter))
#m.pack(side=TOP, expand=YES)
#m.config(bg="#3366ff", justify=CENTER, font=("calibri", 29))
label.config(text=str(counter), bg="#000000", justify=CENTER, font=("calibri", 29))
label.pack(side=TOP)
count()



# Create functions to run when the buttons are pressed
def Input_1(channel):
# Put whatever Button 1 does in here
print ('Button 1')
counter_label(label);

def Input_2(channel):
# Put whatever Button 2 does in here
print ('Button 2');

def Input_3(channel):
# Put whatever Button 3 does in here
print ('Button 3');

class SplashScreen(Frame):
def __init__(self, master=None, width=0.8, height=0.6, useFactor=True):
Frame.__init__(self, master)
self.pack(side=TOP, fill=BOTH, expand=YES)

# get screen width and height
ws = self.master.winfo_screenwidth()
hs = self.master.winfo_screenheight()
w = (useFactor and ws*width) or width
h = (useFactor and ws*height) or height
# calculate position x, y
x = (ws) - (w)
y = (hs) - (h)
self.master.geometry('%dx%d+%d+%d' % (ws, hs, 0, 0))

self.master.overrideredirect(True)
self.lift()

# Wait for Button 1 to be pressed, run the function in "callback" when it does, also software debounce for 300 ms to avoid triggering it multiple times a second
GPIO.add_event_detect(BUTTON_1, GPIO.BOTH, callback=Input_1, bouncetime=200)
GPIO.add_event_detect(BUTTON_2, GPIO.BOTH, callback=Input_2, bouncetime=200) # Wait for Button 2 to be pressed
GPIO.add_event_detect(BUTTON_3, GPIO.BOTH, callback=Input_3, bouncetime=200) # Wait for Button 3 to be pressed


root = tk.Tk()
#root.attributes('-fullscreen', True)

sp = SplashScreen(root)
sp.config(bg="#000000")

sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()
print ("sw:", sw)
print ("sh:", sh)

Button(sp, text="Press this button to kill the program", bg='red', command=root.destroy).pack(side=BOTTOM, fill=X)

root.title("Counting Seconds")
label = tk.Label(root, fg="green")
label.pack()

root.mainloop()


# Start a loop that never ends
#while True:
# Put anything you want to loop normally in here
# sleep(.1); # Sleep for a full minute, any interrupt will break this so we are just saving cpu cycles.

最佳答案

您必须将 SplashScreen 作为标签父级,除了将它们移动到中心并为您提供相对位置之外,您不应该使用 pack() 作为标签:

label = tk.Label(sp, fg="green")
label.place(relx=0.5, rely=0.5, anchor=CENTER)

还修改了 counter_label 函数

counter = 0 
def counter_label(label):
def count():
global counter
counter += 1
#m = Label(text=str(counter))
#m.pack(side=TOP, expand=YES)
#m.config(bg="#3366ff", justify=CENTER, font=("calibri", 29))
label.config(text=str(counter), bg="#000000", justify=CENTER, font=("calibri", 29))
count()

enter image description here

关于python - Raspberry Pi、tkinter、触发器上的屏幕计数器、图像问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44248080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com