[Python Confession Series] Create an irresistible confession interface (complete code)

An irresistible confession interface

Pop-up interface during runtime
Confession interface 1
Pop-up when "Don't" is clicked
Confession interface 2
Pop-up when "×" is clicked
Confession interface 3

environmental needs

  • python3.11.4 and above
  • PyCharm Community Edition 2023.2.5
  • pyinstaller6.2.0 (Optional, this library is used for packaging so that the program does not have a python environment It can also be run. If you want to send it to a good friend, you need this library~)

【Note】

Complete code

import tkinter as tk
import tkinter.messagebox
root = tk.Tk()
root.title('❤')
root.resizable(0, 0)
root.wm_attributes("-toolwindow", 1)
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
widths = 300
heights = 100
x = (screenwidth - widths) / 2
y = (screenheight - heights) / 2
root.geometry('%dx%d+%d+%d' % (widths, heights, x, y))  # 设置在屏幕中居中显示
tk.Label(root, text='亲爱的,做我女朋友好吗?', width=37, font=('宋体', 12)).place(x=0, y=10)


def OK():  # 同意按钮
    root.destroy()
    # 同意后显示漂浮爱心


def NO():  # 拒绝按钮,拒绝不会退出,必须同意才可以退出哦~
    tk.messagebox.showwarning('❤', '再给你一次机会!')


def closeWindow():
    tk.messagebox.showwarning('❤', '逃避是没有用的哦')


tk.Button(root, text='好哦', width=5, height=1, command=OK).place(x=80, y=50)
tk.Button(root, text='不要', width=5, height=1, command=NO).place(x=160, y=50)
root.protocol('WM_DELETE_WINDOW', closeWindow)  # 绑定退出事件
root.mainloop()

Detailed analysis

This is a simple GUI program implemented using Python's tkinter library. The purpose is to use a small window to woo the user and ask the other person to become his girlfriend.

First, let’s introduce the basic framework of the code:

  • Import tkinter library
  • Create a tk object, which is the window
  • Add a Label and two Buttons to the window
  • Define callback functions for two Buttons
  • Call the mainloop() function to start displaying the window

Let's analyze the code in detail:

  1. Import tkinter library

Python's tkinter library is a convenient and easy-to-use GUI library for creating windows and various GUI components, such as Label, Button, Entry, etc. You need to import the tkinter library before use.

import tkinter as tk
import tkinter.messagebox

The tkinter library is imported and renamed to tk so that its functions can be called more conveniently.

  1. Create a tk object, which is the window

Create a window object in the program:

root = tk.Tk()

Among them, root is the name of the window object, which can be defined by yourself. This line of code creates a window object named root.

Next set the title, size and position for the window:

root.title('❤')
root.resizable(0, 0)
root.wm_attributes("-toolwindow", 1)
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
widths = 300
heights = 100
x = (screenwidth - widths) / 2
y = (screenheight - heights) / 2
root.geometry('%dx%d+%d+%d' % (widths, heights, x, y))
  • Set the title: Use the title() function to set the title of the window. Here, the title is set to a heart shape.
  • Set the size and position: Use the geometry() function to set the size and position of the window. Here, the window is set to 300 in width and 100 in height, and the window is displayed in the center of the screen.

The code for setting the window size and position is relatively complicated and can be explained briefly:

  • Get the width and height of the current screen: Use the winfo_screenwidth() and winfo_screenheight() functions to get the width and height of the screen respectively.
  • Set the width and height of the window: Set the width of the window to 300 and the height to 100.
  • Calculate the position of the window: Calculate the coordinates (x, y) of the upper left corner of the window so that the window is displayed in the middle of the screen.

The following two lines of code are also set:

root.resizable(0, 0)
root.wm_attributes("-toolwindow", 1)
  • Disable window resizing: Use the resizable() function to set whether the window can be resized. Here it is set to unchangeable.
  • Set the window as a tool window: Use the wm_attributes() function to set the attributes of the window. Here, set it as a tool window, so that the window cannot be displayed in the taskbar.
  1. Add a Label and two Buttons to the window

Add Label and Button to the window:

tk.Label(root, text='亲爱的,做我女朋友好吗?', width=37, font=('宋体', 12)).place(x=0, y=10)
tk.Button(root, text='好哦', width=5, height=1, command=OK).place(x=80, y=50)
tk.Button(root, text='不要', width=5, height=1, command=NO).place(x=160, y=50)
  • Add Label: Use the Label() function to create a Label component, set the text content to "Dear, will you be my girlfriend?", set the width to 37, the font size to 12, and then display it in the window, position is (x,y)=(0,10).
  • Add Button: Use the Button() function to create two Button components, set the text content to "Okay" and "No" respectively, the size to 5×1, set the callback functions to OK and NO, and finally display them in the window respectively. The positions (x,y)=(80,50) and (x,y)=(160,50).
  1. Define callback functions for two Buttons

Define callback functions for two Buttons:

def OK():  
    root.destroy()
    # 同意后显示漂浮爱心

def NO():  
    tk.messagebox.showwarning('❤', '再给你一次机会!')

def closeWindow():
    tk.messagebox.showwarning('❤', '逃避是没有用的哦')

Three functions are defined here, namely OK, NO, and closeWindow. in:

  • OK function: When the user clicks the "OK" button, the destroy() function is called to close the window and express agreement to become the boyfriend and girlfriend. In addition, you can add some code to this function, such as displaying floating hearts.
  • NO function: When the user clicks the "No" button, the showwarning() function is called to display a pop-up window, prompting the user to give another chance. Note that the window will not close at this time and you must agree to exit.
  • closeWindow function: When the user clicks the close button in the upper right corner of the window, the showwarning() function is called to pop up a window to remind the user that escaping is useless.
  1. Call the mainloop() function to start displaying the window

Call the mainloop() function to start displaying the window and wait for user operations.

root.mainloop()

When the user clicks the "OK" button, the window closes and the program ends. When the user clicks the "Don't" button, the window will not close and continues to wait for user action. When the user clicks the close button in the upper right corner of the window, after the prompt box pops up, the window will not close and continues to wait for the user's operation.

This is the code and logic of the entire program. Although it is a simple example, it reflects the basic usage of the tkinter library and is also quite interesting.

Series of articles

Python confession series Article directory direct link
1 An irresistible confession interface https://want595.blog.csdn.net/article/details/134744894
2 Unlimited pop-up confession code https://want595.blog.csdn.net/article/details/134744711
3 Confession code with words floating all over the screen https://want595.blog.csdn.net/article/details/135037388
4 beating heart https://want595.blog.csdn.net/article/details/134744191
5 floating hearts https://want595.blog.csdn.net/article/details/134744929
6 love light wave https://want595.blog.csdn.net/article/details/134747365
7 meteor shower https://want595.blog.csdn.net/article/details/134747408
8 rose flower https://want595.blog.csdn.net/article/details/134747447

Guess you like

Origin blog.csdn.net/m0_68111267/article/details/134744894