我有一个简单的脚本,在屏幕底部显示一个kill按钮,在触发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.
答案 0 :(得分:1)
您必须将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()