How to cerate splash screen in Tkinter Python Part -10 || Tkinter Series ||
ฝัง
- เผยแพร่เมื่อ 5 ก.ย. 2024
- In this Python tutorial, part 10 of the Tkinter series, you'll learn how to create a splash screen for your graphical user interface (GUI) applications built with Tkinter. A splash screen is a temporary introductory screen that appears while your main application loads. This video will guide you through the process of creating a splash screen in Tkinter, likely covering aspects like:
Setting up a new Tkinter window for the splash screen
Adding visual elements like logos, text labels, or progress bars
Controlling the display time of the splash screen
Destroying the splash screen and showing your main application window
This video is beneficial for programmers who want to enhance the user experience of their Tkinter applications by adding a professional-looking splash screen.
The video is part of a Tkinter series, so if you're new to Tkinter, consider watching previous parts for a foundational understanding of Tkinter GUI development.
Explanation
1. '# How to create splash screen using tkinter': A comment describing the purpose of the code.
2. 'import tkinter # Import the tkinter library': Imports the 'tkinter' library for GUI development.
3. 'from tkinter import * # Import everything from tkinter': Imports all tkinter classes and functions.
4. (Empty line)
5. 'class Splash_Screen: # Define a class named Splash_Screen': Defines a class named 'Splash_Screen'.
6. ' def __init__(self, root): # Define the constructor': Defines the constructor method for the class.
7. ' self.root = root # Assign the root window to the object': Assigns the main window to an instance variable.
8.
9. ' # Create a new top-level window for the splash screen': A comment explaining the creation of the splash screen window.
10. ' self.splash_screen = Toplevel(self.root)': Creates a new top-level window for the splash screen.
11. ' self.root.iconify() # Hide the main application window': Hides the main application window.
12. ' self.splash_screen.attributes("-fullscreen", True) # Set splash screen to fullscreen': Sets the splash screen window to fullscreen.
13.
14. ' # Variables for animation and logic': A comment explaining the following variables.
15. ' self.text = '.'': A variable for the animation text.
16. ' self.count = 1': A counter for the animation.
17. ' self.multiplier = 1': A multiplier for the animation effect.
18. ' self.timer = 30': A variable to control the duration of the animation.
19.
20. ' # Create a label with text and style': A comment explaining the creation of a label.
21. ' self.label1 = Label(self.splash_screen, text="Loading", width=35, font=("Arial", 100, 'bold'))': Creates a label with specific text and style.
22. ' self.label1.pack() # Pack the label onto the splash screen window': Adds the label to the splash screen window.
23.
24. ' # Start the event loop for the splash screen': A comment explaining the event loop.
25. ' self.splash_screen.mainloop()': Starts the event loop for the splash screen.
26.
27. ' def load_text(self, label1): # Define a method to update text and animation': Defines a method to update the text for the animation.
28. ' if self.multiplier greater than 5: # Check if multiplier needs reset': Checks if the multiplier exceeds 5.
29. ' self.multiplier = 1': Resets the multiplier if it exceeds 5.
30. ' self.label1['text'] = 'Loading' + self.text * self.multiplier + " " * (6 - self.multiplier) # Update label text with animation': Updates the label text with the animation effect.
31.
32. ' if self.count greater than self.timer: # Check if animation duration is reached': Checks if the animation duration has been reached.
33. ' self.splash_screen.after(300, self.load_text, self.label1) # Schedule next animation cycle': Schedules the next animation update after 300 milliseconds.
34. ' else: # If not finished animating': If the animation is still running.
35. ' self.splash_screen.destroy() # Destroy the splash screen': Destroys the splash screen window.
36. ' self.root.deiconify() # Unhide the main application window': Unhides the main application window.
37. ' self.count += 1 # Increment animation counter': Increments the animation counter.
38. ' self.multiplier += 1 # Increment animation multiplier': Increments the animation multiplier.
39.
40. '# Testing - Create main window and splash screen object': A comment explaining the creation of the main window and splash screen object.
41. 'app = Tk()': Creates the main application window.
42. 'Splash_Screen(app)': Creates an instance of 'Splash_Screen' with the main window.
43. 'app.mainloop()': Starts the event loop for the main application window.