[英]How to access to widgets properties in a Gtk+ 3 / Glade / Python 3 project
[英]Python, Glade, GTK+
我正在為我的 GTK GUI 苦苦掙扎……
我為燈光等制作了一個 GUI,到目前為止“一切”都可以工作,但我無法弄清楚它是否有一個計時器,它將在給定時間啟動燈光並結束它。 左側的旋轉按鈕用於“開始時間”,右側的旋轉按鈕用於“結束時間”。 每個 object 都有一個用於啟用/禁用計時器的復選框,每次輸入或編輯時間時,都需要按下確定按鈕。 有人能幫助我嗎?
import datetime
import gi
import time
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk as gtk, GObject, GLib
class Main:
def __init__(self):
gladeFile = "main.glade"
self.builder = gtk.Builder()
self.builder.add_from_file(gladeFile)
self.builder.connect_signals(self)
self.window = self.builder.get_object("Main")
self.window.connect("delete-event", gtk.main_quit)
self.window.show_all()
self.window.set_decorated(False)
self.labelClock = self.builder.get_object("Time")
self.labelDate = self.builder.get_object("Date")
self.Uur_LED_Aan = self.builder.get_object("Uur_LED_Aan")
self.Uur_LED_Uit = self.builder.get_object("Uur_LED_Uit")
self.Uur_Licht_Aan = self.builder.get_object("Uur_Licht_Aan")
self.Uur_Licht_Uit = self.builder.get_object("Uur_Licht_Uit")
self.Uur_Luik_Open = self.builder.get_object("Uur_Luik_Open")
self.Uur_Luik_Dicht = self.builder.get_object("Uur_Luik_Dicht")
def compare(self):
ULA = self.OK()
ULU = self.OK()
start_hour = ULA
start_minute = '00'
end_hour = ULU
end_minute = '00'
start_time = int(start_hour) * 60 + int(start_minute)
end_time = int(end_hour) * 60 + int(end_minute)
current_time = datetime.datetime.now().hour * 60 + datetime.datetime.now().minute
if start_time == current_time:
print("LED ON")
elif end_time == current_time:
print("LED OFF")
return True
def ULED_Update(self):
GLib.timeout_add(100, self.compare, True)
def OK(self, button):
ULA = self.Uur_LED_Aan.get_value()
ULU = self.Uur_LED_Uit.get_value()
ULIA = self.Uur_Licht_Aan.get_value()
ULIU = self.Uur_Licht_Uit.get_value()
ULO = self.Uur_Luik_Open.get_value()
ULD = self.Uur_Luik_Dicht.get_value()
print(ULA, ULU, ULIA, ULIU, ULO, ULD)
return ULA, ULU, ULIA, ULIU, ULO, ULD
def displayDate(self, state):
Time_now = str(datetime.datetime.now().strftime("%d-%m-%Y"))
self.labelDate.set_label(Time_now)
return state,
def displayclock(self, state):
# putting our datetime into a var and setting our label to the result.
# we need to return "True" to ensure the timer continues to run, otherwise it will only run once.
Time_now = str(datetime.datetime.now().strftime("%H:%M:%S"))
self.labelClock.set_label(Time_now)
return state, Time_now
# Initialize Timer
def startclocktimer(self):
# this takes 2 args: (how often to update in millisec, the method to run)
GLib.timeout_add(100, self.displayclock, True)
def startDate(self):
# this takes 2 args: (how often to update in millisec, the method to run)
GLib.timeout_add(100, self.displayDate, True)
def Switch_LED(self, switch, gparam):
if switch.get_active():
state = "Led_On"
else:
state = "Led_Off"
print("Led_Switch was", state)
def Switch_Licht(self, switch, gparam):
if switch.get_active():
state = "Licht_on"
else:
state = "Licht_off"
print("Licht_Switch was", state)
def Luik_Open(self, button):
state = "Rolluik_open"
print(state)
def Luik_Dicht(self, button):
state = "Rolluik_dicht"
print(state)
if __name__ == '__main__':
main = Main()
main.startclocktimer()
main.startDate()
main.ULED_Update()
gtk.main()
啟動一個單獨的線程來更新計時器,在 GUI 的 label 中顯示時間。 我看到一個例子https://github.com/f4iteightiz/UWR_scoreboard
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.