1-
2- from tkinter import *
3- from tkcalendar import Calendar
41import tkinter as tk
5-
6-
7- window = tk .Tk ()
8-
9- # Adjust size
10- window .geometry ("600x500" )
11-
12- gameList = ["Game List:" ]
13- # Change the label text
14- def show ():
15- game = selected1 .get () + " vs " + selected2 .get ()+ " on " + cal .get_date ()
16- gameList .append (game )
17- #print(gameList)
18- gameListshow = "\n " .join (gameList )
19- #print(gameList)
20- label .config (text = gameListshow )
21-
22-
23- # Dropdown menu options
24- options = [
25- "Team 1" ,
26- "Team 2" ,
27- "Team 3" ,
28- "Team 4" ,
29- "Team 5" ,
30- "Team 6"
31- ]
32-
33- # datatype of menu text
34- selected1 = StringVar ()
35- selected2 = StringVar ()
36-
37- # initial menu text
38- selected1 .set ("Team 1" )
39- selected2 .set ("Team 2" )
40-
41- # Create Dropdown menu
42- L1 = Label (window , text = "Visitor" )
43- L1 .place (x = 40 , y = 35 )
44- drop1 = OptionMenu (window , selected1 , * options )
45- drop1 .place (x = 100 , y = 30 )
46-
47- L2 = Label (window , text = "VS" )
48- L2 .place (x = 100 , y = 80 )
49-
50- L3 = Label (window , text = "Home" )
51- L3 .place (x = 40 , y = 115 )
52- drop2 = OptionMenu (window , selected2 , * options )
53- drop2 .place (x = 100 , y = 110 )
54-
55- # Add Calendar
56- cal = Calendar (window , selectmode = 'day' ,
57- year = 2022 , month = 12 ,
58- day = 1 )
59-
60- cal .place (x = 300 , y = 20 )
61-
62-
63-
64- # Create button, it will change label text
65- button = Button ( window , text = "Add to calender" , command = show ).place (x = 100 ,y = 200 )
66-
67- # Create Label
68- label = Label (window , text = " " )
69- label .place (x = 150 , y = 250 )
70-
71- window .mainloop ()
2+ from tkinter import StringVar , Label , Button , OptionMenu
3+ from tkcalendar import Calendar # Install via: pip install tkcalendar
4+
5+ def main () -> None :
6+ """Create and run the sports schedule management application"""
7+ # Create main application window
8+ window = tk .Tk ()
9+ window .title ("Sports Schedule Manager" )
10+ window .geometry ("600x500" )
11+ window .resizable (True , True ) # Allow window resizing
12+
13+ # Initialize list to store scheduled games
14+ game_list : list [str ] = ["Game List:" ]
15+
16+ # Available teams for dropdown selection
17+ team_options : list [str ] = [
18+ "Team 1" , "Team 2" , "Team 3" ,
19+ "Team 4" , "Team 5" , "Team 6"
20+ ]
21+
22+ # Create and arrange all GUI components
23+ create_widgets (window , game_list , team_options )
24+
25+ # Start the main event loop
26+ window .mainloop ()
27+
28+ def create_widgets (
29+ window : tk .Tk ,
30+ game_list : list [str ],
31+ team_options : list [str ]
32+ ) -> None :
33+ """Create and position all GUI widgets in the main window"""
34+ # Variables to store selected teams
35+ visitor_var : StringVar = StringVar (window )
36+ home_var : StringVar = StringVar (window )
37+
38+ # Set default selections
39+ visitor_var .set (team_options [0 ])
40+ home_var .set (team_options [1 ])
41+
42+ # Configure grid weights for responsive layout
43+ window .columnconfigure (0 , weight = 1 )
44+ window .columnconfigure (1 , weight = 1 )
45+ window .rowconfigure (0 , weight = 1 )
46+ window .rowconfigure (1 , weight = 1 )
47+ window .rowconfigure (2 , weight = 3 )
48+
49+ # Create left frame for team selection
50+ left_frame = tk .Frame (window , padx = 10 , pady = 10 )
51+ left_frame .grid (row = 0 , column = 0 , rowspan = 2 , sticky = "nsew" )
52+
53+ # Visitor team selection
54+ visitor_label = Label (left_frame , text = "Visitor:" )
55+ visitor_label .grid (row = 0 , column = 0 , sticky = "w" , pady = 5 )
56+
57+ visitor_dropdown = OptionMenu (left_frame , visitor_var , * team_options )
58+ visitor_dropdown .grid (row = 0 , column = 1 , sticky = "ew" , pady = 5 )
59+
60+ # Home team selection
61+ home_label = Label (left_frame , text = "Home:" )
62+ home_label .grid (row = 1 , column = 0 , sticky = "w" , pady = 5 )
63+
64+ home_dropdown = OptionMenu (left_frame , home_var , * team_options )
65+ home_dropdown .grid (row = 1 , column = 1 , sticky = "ew" , pady = 5 )
66+
67+ # Create calendar frame on the right
68+ right_frame = tk .Frame (window , padx = 10 , pady = 10 )
69+ right_frame .grid (row = 0 , column = 1 , rowspan = 2 , sticky = "nsew" )
70+
71+ calendar = Calendar (
72+ right_frame ,
73+ selectmode = 'day' ,
74+ year = 2023 ,
75+ month = 7 ,
76+ day = 16
77+ )
78+ calendar .pack (fill = "both" , expand = True )
79+
80+ # Create game list display area
81+ display_frame = tk .Frame (window , padx = 10 , pady = 10 )
82+ display_frame .grid (row = 2 , column = 0 , columnspan = 2 , sticky = "nsew" )
83+
84+ # Text widget with scrollbar for game list
85+ game_display = tk .Text (display_frame , wrap = tk .WORD , height = 10 )
86+ game_display .pack (side = tk .LEFT , fill = "both" , expand = True )
87+
88+ scrollbar = tk .Scrollbar (display_frame , command = game_display .yview )
89+ scrollbar .pack (side = tk .RIGHT , fill = tk .Y )
90+ game_display .config (yscrollcommand = scrollbar .set )
91+
92+ # Initialize display with empty list
93+ update_game_display (game_display , game_list )
94+
95+ # Add to schedule button - pass game_display to add_game
96+ add_button = Button (
97+ left_frame ,
98+ text = "Add to Schedule" ,
99+ command = lambda : add_game (
100+ window , game_list , visitor_var , home_var , calendar , game_display
101+ )
102+ )
103+ add_button .grid (row = 2 , column = 0 , columnspan = 2 , pady = 20 )
104+
105+ # Configure weights for responsive resizing
106+ left_frame .columnconfigure (1 , weight = 1 )
107+ right_frame .columnconfigure (0 , weight = 1 )
108+ right_frame .rowconfigure (0 , weight = 1 )
109+ display_frame .columnconfigure (0 , weight = 1 )
110+ display_frame .rowconfigure (0 , weight = 1 )
111+
112+ def add_game (
113+ window : tk .Tk ,
114+ game_list : list [str ],
115+ visitor_var : StringVar ,
116+ home_var : StringVar ,
117+ calendar : Calendar ,
118+ game_display : tk .Text # Added game_display parameter
119+ ) -> None :
120+ """Add a new game to the schedule and update the display"""
121+ # Get selected values
122+ visitor = visitor_var .get ()
123+ home = home_var .get ()
124+ date = calendar .get_date ()
125+
126+ # Validate input (prevent same team match)
127+ if visitor == home :
128+ show_error (window , "Error" , "Visitor and home teams cannot be the same!" )
129+ return
130+
131+ # Create game entry and add to list
132+ game_entry = f"{ visitor } vs { home } on { date } "
133+ game_list .append (game_entry )
134+
135+ # Update the display with new list
136+ update_game_display (game_display , game_list )
137+
138+ def update_game_display (display : tk .Text , game_list : list [str ]) -> None :
139+ """Update the text widget with current game list"""
140+ # Clear existing content
141+ display .delete (1.0 , tk .END )
142+ # Insert updated list
143+ display .insert (tk .END , "\n " .join (game_list ))
144+
145+ def show_error (window : tk .Tk , title : str , message : str ) -> None :
146+ """Display an error message in a modal dialog"""
147+ error_window = tk .Toplevel (window )
148+ error_window .title (title )
149+ error_window .geometry ("300x150" )
150+ error_window .resizable (False , False )
151+
152+ # Center error window over main window
153+ error_window .geometry ("+%d+%d" % (
154+ window .winfo_rootx () + window .winfo_width () // 2 - 150 ,
155+ window .winfo_rooty () + window .winfo_height () // 2 - 75
156+ ))
157+
158+ # Error message label
159+ message_label = Label (error_window , text = message , padx = 20 , pady = 20 )
160+ message_label .pack (fill = "both" , expand = True )
161+
162+ # Close button
163+ close_button = Button (error_window , text = "OK" , command = error_window .destroy )
164+ close_button .pack (pady = 10 )
165+
166+ # Make dialog modal
167+ error_window .transient (window )
168+ error_window .grab_set ()
169+ window .wait_window (error_window )
170+
171+ if __name__ == "__main__" :
172+ main ()
0 commit comments