Write in front
Python implements the complete code of unlimited pop-up windows and full screen confession code.
Technical requirements
-
tkinter
Graphical user interface:- Create windows, set window size and position (
geometry
)。 - use
Label
The control displays text and heart-shaped characters, and sets background color, font and style.
- Create windows, set window size and position (
-
Multithreading technology (
threading
):- pass
Thread
Create multiple threads so that windows can be generated and displayed simultaneously. - use
setDaemon(True)
Set the thread as a daemon thread to ensure that the child thread can also terminate when the main thread ends.
- pass
-
Random number generation (
random
):- Utilize
Randomly generate the screen position of small windows to increase dynamics and fun.
- Utilize
-
Time control (
time
):- use
(0.1)
Set the time interval for small window generation to form a gradually appearing animation effect.
- use
-
Screen size calculation:
- use
winfo_screenwidth
andwinfo_screenheight
Dynamically obtain the screen width and height to ensure that the window display is suitable for various resolutions.
- use
Complete code
import tkinter as tk import random as ra import threading as td import time as ti def Love(): root=() width=200 height=50 screenwidth=root.winfo_screenwidth() screenheight=root.winfo_screenheight() x=(0,screenwidth) y=(0,screenheight) ("❤") ("%dx%d+%d+%d"%(width,height,x,y)) (root,text='I LOVE YOU!',fg='white',bg='pink',font=("Comic Sans MS",15),width=30,height=5).pack() () ……
Code Analysis
This code is passed through Python'stkinter
The module implements an interesting "confession" effect. After the program is run, multiple small windows will be randomly generated on the screen. Each window displays the text of "I LOVE YOU!". At the same time, the background is pink, the font is white, the text style is lively, and it has a romantic visual effect. In addition, the code also contains a window showing the heart-shaped character "♥" on the large screen. The whole effect is very vivid. The following is a detailed analysis of this code.
1. Functional Overview
The main functions of this code are:
-
Generate small windows with multiple random locations:
Each window contains "I LOVE YOU!” text, showing romantic confession information. -
Create a window showing the heart-shaped character "♥" in full screen:
This window is larger in size and is located in the center of the screen, with a prominent visual impact. -
Multi-threaded operation:
The program uses threaded methods to control the generation of small windows and large windows, so that multiple windows can be displayed at the same time, improving operational efficiency and fluency.
2. Code structure and logic analysis
The overall structure of the code includes the following parts:
-
Love
function:
Implement the generation of small windows. -
Heart
function:
Implement the generation of large windows. -
Main thread logic:
Create a large window thread and continuously generate multiple small windows in the main thread.
2.1 Love
function
This is the generated function of the small window. Its main function is to create a small window with random locations that displays "I LOVE YOU!” text. The key points are as follows:
Window size:
width = 200 height = 50
The size of each window is fixed to 200 pixels wide and 50 pixels high, ensuring the compactness and elaboration of the window.
Random location:
x = (0, screenwidth) y = (0, screenheight)
pass
Random location of the function generation window,
x
andy
It is the coordinates of the upper left corner of the window. This allows the window to appear in different positions every time, adding to the fun.Window title and content:
("❤") (root, text='I LOVE YOU!', fg='white', bg='pink', font=("Comic Sans MS", 15), width=30, height=5).pack()
Set the window title to "♥" to add romantic atmosphere;
Label
The control has the background color to pink (bg='pink'
), the font color is white (fg='white'
), using the font "Comic Sans MS", the text style is lively.Window layout and startup:
("%dx%d+%d+%d" % (width, height, x, y)) ()
pass
geometry
Method sets the window size and position, callmainloop
Start the window.
2.2 Heart
function
This is a large window generation function, which creates a window that displays the heart-shaped character "♥" in full screen. The key points are as follows:
Window size and centered position:
width = 600 height = 400 x = (screenwidth - width) // 2 y = (screenheight - height) // 2
The large window has a width of 600 pixels and a height of 400 pixels. Its position is calculated as centered.
Show heart-shaped characters:
(root, text='❤', fg='pink', bg='white', font=("Comic Sans MS", 500), width=300, height=20).pack()
The character "♥" uses a pink font with a white background, with a font size of 500, and the display is huge and conspicuous.
2.3 Main thread logic
The main thread mainly completes two tasks: start a large window thread and continuously creates small window threads in the loop.
Start large window thread:
t = (target=Heart) (True) # Set up daemon thread()
A thread was started in the main thread
t
Come to executeHeart
Function. set upsetDaemon(True)
, means that when the main thread ends, the daemon thread will also terminate, avoiding the program from hanging.Looping to create small window threads:
The main thread creates 50 threads through a loop, and each thread executes separately.Love
Function. pass(0.1)
Implement a small window every 0.1 seconds, and the effect shows a dynamic process of gradually appearing in the window.
3. Technical Highlights
3.1 Multi-threading
Multithreading technology is used extensively in the code, so that multiple windows can be displayed and respond to user operations at the same time:
- Each thread runs independently
Love
orHeart
function, thereby generating window. - pass
setDaemon(True)
Set the thread as a daemon thread to ensure that the program can exit normally.
3.2 Randomity
Usedrandom
The module generates a small window with random locations:
- pass
Randomly select the coordinates on the screen to ensure that the location of each window appears differently, and increase the fun and visual impact.
3.3 Interface design
The code is fully utilizedtkinter
Provided controls and properties:
-
Label
Used to display text and characters. -
geometry
Set the size and position of the window. - Color matching (pink background, white font) and font design (Comic Sans MS) enhance the romance.
4. Advantages and Highlights
Simple and easy to understand:
The code logic is clear, throughtkinter
Achieve an intuitive and effective effect.Multi-threaded enhanced experience:
Use multiple threads to generate multiple windows at the same time, making the dynamic effect smoother and more expressive.Randomness and personalization:
The window position is randomly generated, making the effect of each run of the program different, increasing the fun.Interface design is interesting:
The characters "♥" and confession text are combined with romantic color matching, which has good visual effects and emotional expression.
Summarize
This code passestkinter
A unique confession effect is realized, fully demonstrating the flexibility of multi-threading and the dynamic effects generated randomly. Its romantic design and vivid effects make the program not only fun, but also used for confessions or creative displays in actual scenes.
The above is the detailed content of the sample code to use Python to achieve unlimited pop-up windows and full-screen confession. For more information about Python unlimited pop-up window confession, please pay attention to my other related articles!