2017-05-29 8 views
0

私はGPIOがトリガされたときに画面の一番下にキルボタンとカウンタを表示するシンプルなスクリプトを持っています。カウンターを画面中央に表示したいです。しかし、実際には、 "殺す"ボタンを押して、その下にカウンタを表示します。カウンターを画面中央に表示するにはどうしたらいいですか?Raspberry Pi、tkinter、トリガ時のスクリーンカウンタon

起動画面

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. 

答えて

1

後の画面ラベルに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

を修正