Python İle Hava Durumuna Göre Masaüstü Arka Planını Değiştirme

0 932

Python programlama dilini kullanarak bir çok farklı uygulama yapılabilir. Bu örneğimizde ise Python ile  hava durumu verilerini çekerek masaüstü arkaplan resmini değiştireceğiz. Hava durumu verilerinin yanında günün hangi saatinde olduğumuza göre de arkaplan resminde değişiklik yapacağız.

Sitemizden Python dersleri ile programlamaya adım atabilir, 100 çözümlü algoritma sorusu ile kendiniz geliştirebilirsiniz.

Pyhon İle Masaüstü Arkaplanını Değiştirme

Bu uygulama için reguest, datetime, ctypes ve time kütüphanlerini kullanıyoruz. Hava durumunu openweathermap.org sitesinden çekiyoruz.

city değişkenine yaşadığınız şehrin adını küçük harflerle yazmanız gerekiyor.

Kodu çalıştırmadan önce arkaplan olarak kullanacağınız resimleri seçip kodun içinde ilgili yerleri değiştirmeyi unutmayın.

import requests
import ctypes
import datetime
import time
from datetime import date


SPI_SETDESKWALLPAPER = 20
path_to_folder = r"C:\Users\Halil\Belgeler\Resimler\Backgrounds\Python"
#Buraya arka plan resimlerimizin bulunduğu klasörümüzün tam yolunu ekliyoruz.

api_address='http://api.openweathermap.org/data/2.5/weather?appid=0c42f7f6b53b244c78a418f4f181282a&q='
city = "YAŞADIĞINIZ ŞEHRİN ADINI YAZIN"

url = api_address + city


def main():
    timestamp = datetime.datetime.now().time()
    start_night = datetime.time(18, 1)
    end_night = datetime.time(6, 0)
    start_day = datetime.time(6, 1)
    end_day = datetime.time(18, 0)
    json_data = requests.get(url).json()
    format_data = json_data["weather"][0]["main"]
    print("Looped")

    if date.today().weekday() == 6:
        ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, path_to_folder + "\Tokyo.jpg", 3)
        time.sleep(120)
        print("Sunday")
        main()
    if format_data == "Rain":
        ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, path_to_folder + "\Waterfall.jpg", 3)
        time.sleep(120)
        print("Rain")
        main()
    elif format_data == "Thunderstorm":
        ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, path_to_folder + "\Cloud.jpg", 3)
        time.sleep(120)
        print("Storm")
        main()
    elif format_data == "Drizzle":
        ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, path_to_folder + "\Rooftop1.png", 3)
        time.sleep(120)
        print("Drizzle")
        main()

# Night & Day
    elif format_data == "Clear" and start_night <= timestamp or timestamp <= end_night:
        ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, path_to_folder + "\Moon.png", 3)
        time.sleep(120)
        print("Night")
        main()
    elif format_data == "Clear" and start_day <= timestamp <= end_day:
        ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, path_to_folder + "\KatanaMorning.png", 3)
        time.sleep(120)
        print("Day")
        main()

    elif format_data == "Clouds":
        ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, path_to_folder + "\Shibuya.png", 3)
        time.sleep(120)
        print("Clouds")
        main()
    else:
        ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, path_to_folder + "\Firewatch.jpg", 3)
        time.sleep(120)
        print("Other")
        main()


main()

 

C# İle masaüstü arkaplanını değiştirme

Yukarıdaki koda benzer biçimde c# kullanarakta masaüstü arkaplanını değiştirebilirsiniz. Altta paylaştığım kod sadece arkaplanı değiştirir.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Runtime.InteropServices;

using System.Windows.Forms;

namespace wallpaper

{

public partial class Form1 : Form

{

[DllImport("user32.dll", CharSet = CharSet.Auto)]

private static extern Int32 SystemParametersInfo(UInt32 uiAction, UInt32 uiParam, String pvParam, UInt32 fWinIni);

private static UInt32 SPI_SETDESKWALLPAPER = 30;

private static UInt32 SPIF_UPDATEINIFILE = 0x1;

private String imageFileName = @"E:\photos\DSC01680.bmp";

public Form1()

{

InitializeComponent();

}

public void button1_Click(object sender, EventArgs e)

{

string wall = imageFileName; //+ "DSC0180.bmp";

Form1 wallset = new Form1();

wallset.SetImage(wall);

}

public void SetImage(string filename)

{

SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, filename, SPIF_UPDATEINIFILE);

}

}

}

 

Yorumlarınızı bekliyoruz.

Cevap bırakın

E-posta hesabınız yayımlanmayacak.

Bu web sitesi deneyiminizi geliştirmek için çerezleri kullanır. Bununla iyi olduğunuzu varsayacağız, ancak isterseniz vazgeçebilirsiniz. Kabul etmek Mesajları Oku