22import pychromecast
33import csv
44import pyautogui
5+ import tkinter as tk
6+ from threading import Thread
7+
8+ global cast
9+ global NameOfCast
10+ global TempsDePause
11+ global Medias
12+ global text1
13+ global text2
14+ global text3
15+ global checkbox_var
516
617with open ("List-Media.csv" , encoding = 'utf-8' , newline = '' ) as csvfile :
718 Medias = list (csv .reader (csvfile ,delimiter = ";" ))
819 print ("Medias link" ,Medias )
920
10- def start_cast ():
11- global cast
12- chromecasts , browser = pychromecast .get_listed_chromecasts (friendly_names = [NameOfCast ])
13- print ("Found {} chromecasts" .format (len (chromecasts )))
14-
15- cast = chromecasts [0 ]
16- cast .wait ()
17- print (cast .status )
21+ def start_cast (stop_connection = False ):
22+ if stop_connection == False :
23+ global cast
24+ chromecasts , browser = pychromecast .get_listed_chromecasts (friendly_names = [NameOfCast ])
25+ print ("Found {} chromecasts" .format (len (chromecasts )))
26+ cast = chromecasts [0 ]
27+ cast .wait ()
28+ print (cast .status )
29+ elif stop_connection == True :
30+ try :
31+ cast .quit_app ()
32+ cast .disconnect ()
33+ print ("Connection closed" , cast .status )
34+ except :
35+ pass
1836
1937
20- # show an image
38+ # Share an image (from a URL) to the Chromecast
2139def show_media (url ):
2240 global cast
2341 mc = cast .media_controller
@@ -29,26 +47,88 @@ def show_media(url):
2947 #
3048 mc .block_until_active ()
3149 return mc .status
32-
50+ # Stop the media playing on the Chromecast and disconnect
51+ """
3352def stop_cast():
3453 global cast
3554 cast.quit_app()
3655 cast.disconnect()
56+ """
57+ # Loop through the images in the list (url(s) inside of csv file)
58+ def loop (number , Repetition = 1 , EndAfterLoop = False ):
59+ for i in range (Repetition ):
60+ for x in range (number ):
61+ show_media (Medias [x ][0 ])
62+ time .sleep (TempsDePause )
63+ if EndAfterLoop == True :
64+ start_cast (stop_connection = True )
65+
66+ ####
67+ #UI
68+ ####
69+ def UI ():
70+ def Send ():
71+ global NameOfCast
72+ global TempsDePause
73+ NameOfCast = text1 .get ()
74+ TempsDePause = int (text2 .get ())
75+ Repetition = int (text3 .get ())
76+ print ("NameOfCast:" ,NameOfCast )
77+ print ("TempsDePause:" ,TempsDePause )
78+ thread = Thread (target = start_and_loop , args = (Repetition ,))
79+ thread .start ()
80+
81+ def start_and_loop (Repetition ):
82+ start_cast ()
83+ if checkbox_var .get () == 1 :
84+ loop (len (Medias ), Repetition , EndAfterLoop = True )
85+ else :
86+ loop (len (Medias ), Repetition )
87+
88+ # create UI
89+ root = tk .Tk ()
90+ root .title ("Chromecast-URL" )
91+ root .geometry ("400x300" )
92+ root .resizable (False , False )
93+ root .configure (background = '#2B2B2B' )
94+
95+ #text
96+ text1_label = tk .Label (root , text = "Name of Chromecast:" )
97+ text1_label .pack ()
98+ text1 = tk .Entry (root )
99+ text1 .pack ()
100+ text2_label = tk .Label (root , text = "Time between photos (s):" )
101+ text2_label .pack ()
102+ text2 = tk .Entry (root )
103+ text2 .pack ()
104+ text3_label = tk .Label (root , text = "Repetition:" )
105+ text3_label .pack ()
106+ text3 = tk .Entry (root )
107+ text3 .pack ()
108+
109+ #create another checkbox
110+ checkbox_var = tk .IntVar (value = 1 )
111+ checkbox1 = tk .Checkbutton (root , text = "End connection after loop" , variable = checkbox_var )
112+ checkbox1 .pack ()
37113
38- def boucle (nombre ):
39- for x in range (nombre ):
40- show_media (Medias [x ][0 ])
41- time .sleep (TempsDePause )
114+ # Create the submit button
115+ submit_button = tk .Button (root , text = "Submit" , command = Send )
116+ submit_button .pack ()
42117
43- ### Main ###
44- NameOfCast = "Mat" #Name of your Chromecast
45- MediasNumber = len ( Medias ) #Number of medias in your list
46- TempsDePause = 5 #Time between each media
47- print ( "MediasNumber" , MediasNumber ) #Debug
118+ def quit ():
119+ print ( "Quit" )
120+ start_cast ( stop_connection = True )
121+ time . sleep ( 1 )
122+ root . destroy ()
48123
49- start_cast ()
124+ # Create the quit button
125+ quit_button = tk .Button (root , text = "Quit and end connection" , command = quit )
126+ quit_button .pack ()
50127
51- boucle (MediasNumber )
128+ # When the window is closed, quit the application
129+ root .protocol ("WM_DELETE_WINDOW" , quit )
52130
53- pyautogui .alert (text = 'Close?' , title = 'Main' , button = 'OK' )
54- stop_cast ()
131+ # Start the main event loop
132+ root .mainloop ()
133+
134+ UI ()
0 commit comments