1
내 코드에서 데스크톱 알림에 gi.repository
을 사용하고 있고 로컬 컴퓨터에서 2 개의 다른 이미지를로드하고 바탕 화면 알림 풍선에 표시되도록 2 개의 다른 기능을 만들었습니다 어떤 조건이 충족되는지. 이 목적을 위해 나는 달성해야 할 것을 보여주기 위해 간단한 코드를 작성했습니다. 내 코드를 가능한 한 깨끗하게 유지하고 그 두 기능을 병합하고 이미지를로드 할 수 있는지 궁금합니다. 나중에 내 코드에서 8 개의 다른 이미지를 사용하고 8 개의 동일한 기능을 사용하는 것이 좋지 않을 수 있습니다.파이썬은 gi.repository를 사용하여 두 가지 기능을 결합합니다.
def weather(arg1, arg2, image_path):
notification = Notify.Notification.new(arg1, arg2)
image = GdkPixbuf.Pixbuf.new_from_file(image_path) # Here
notification.set_icon_from_pixbuf(image)
notification.set_image_from_pixbuf(image)
notification.show()
그것을 사용 : 두 함수 사이의 차이 유일한 이미지 경로이므로
import gi
gi.require_version("Notify", "0.7")
from gi.repository import Notify, GdkPixbuf
def sunny(arg1, arg2):
notification = Notify.Notification.new(arg1, arg2)
image = GdkPixbuf.Pixbuf.new_from_file("_sunny_day.png")
notification.set_icon_from_pixbuf(image)
notification.set_image_from_pixbuf(image)
notification.show()
def cloudy(arg1, arg2):
notification = Notify.Notification.new(arg1, arg2)
image = GdkPixbuf.Pixbuf.new_from_file("_cloudy_day.png")
notification.set_icon_from_pixbuf(image)
notification.set_image_from_pixbuf(image)
notification.show()
while 1:
var1 = 'Something will be here, maybe URL'
if var1 == 'Sunny':
sunny('Arg1', 'Arg2')
elif var1 == 'Cloudy':
cloudy('Arg1', 'Arg2')
나는 그렇게 생각한다! 너는 내 하루를 보냈다. 나는 잠깐 생각조차하지 않았다. 고마워요 – uzdisral
@uzdisral 문제 없습니다. 중복 코드로 자신을 발견하자마자 다른 점을 살펴보고 함수의 새로운 매개 변수로 사용하십시오. 적절한 상황에서이 작업을 수행 할 수 있다면 코드를 실제로 정리하는 방법을 배우게됩니다. – Carcigenicate
유효한 포인트를 가져 왔습니다. 기다릴 수 없어. – uzdisral