Home  >  Article  >  Backend Development  >  How to use Python+tkinter to write a forced confession artifact

How to use Python+tkinter to write a forced confession artifact

PHPz
PHPzforward
2023-05-19 11:31:291147browse

Although Python provides multiple message boxes that can meet the needs of most normal people, they are not flexible enough, so sometimes you have to customize the message box.

New window

The essence of the message box is a window, a piece of information, and two buttons

import tkinter as tk

FONT= ("微软雅黑", 20)
def msgBox(txt, yesFunc=None, noFunc=None):
    win = tk.Tk()
    win.title("started")
    win.geometry("400x200+300+100")
    label = tk.Label(win,text=txt, font=FONT)
    label.pack(side=tk.TOP,expand=tk.YES,fill=tk.BOTH)
    btnYes = tk.Button(win, text="是", width=8,
        font=FONT, command = lambda : yesFunc(win))
    btnYes.pack(side=tk.LEFT,expand=tk.YES,fill=tk.Y)
    btnNo = tk.Button(win, text="否", width=8,font=FONT)
    btnNo.bind("", lambda evt: noFunc(evt, win))
    btnNo.pack(side=tk.RIGHT,expand=tk.YES,fill=tk.Y)

msgBox("你是不是喜欢我?")

FONT is a global variable representing the font Its size will be set through the font parameter when label and button are created.

pack is a layout method. As the name suggests, it is packaging. If you think of the window as a box, then you must pack it wherever you have time. For example, if there is a layer of clothes at the bottom, and there is no room to put them, they can only be placed on the second layer; if only one brick is placed on the second layer, there is still space in other places, so another brick can be placed next to the brick.

In the above case, press the label down from the top, and then place a button on the left and right sides below.

Whether is or is not , the window needs to be operated, so the callback function uses the lambda expression, so that it can window to operate. means that it responds when the mouse floats over the component, and its callback function needs to enter a parameter evt.

The effect is as follows

How to use Python+tkinter to write a forced confession artifact

Close the window

The next is the logic of , that is, after closing the existing window , a new window pops up. The new window only needs to have a

def yesFunc(win):
    x, y = win.winfo_x(), win.winfo_x()
    win.destroy()
    win = tk.Tk()
    win.geometry(f"400x200+{x}+{y}")
    txt = "放学去房后小树林\n不见不散\n嘿嘿嘿"
    label = tk.Label(win,text=txt, font=FONT)
    label.pack(side=tk.TOP,expand=tk.YES,fill=tk.BOTH)
    btn = tk.Button(win, text="好的呀", width=12, font=FONT,
        command=lambda:win.destroy())
    btn.pack(side=tk.TOP)

msgBox("你是不是喜欢我?", yesFunc)

destroy, which means closing the current window. After the current window is closed, create a new window at the location of the original window. win_winfo_x, win_winfo_y are used to obtain the window position.

The effect is as follows

How to use Python+tkinter to write a forced confession artifact

Move the window

Then write the logic of No, when the mouse hovers over No moves the window position when the button is on. Because

import random
def randInt(a, b):
    r = random.randint(-a, a)
    while abs(r) < abs(b):
        r = random.randint(-a, a)
    return r

def noFunc(evt, win):
    x, y = win.winfo_x(), win.winfo_x()
    x += randInt(100, 80)
    y += randInt(30, 20)
    win.geometry(f"400x200+{x}+{y}")

msgBox("你是不是喜欢我?", yesFunc, noFunc)

uses the geometry function, you can not only declare the window position when creating the window, but also modify the window position anytime and anywhere. In this way, when the mouse is floating on the No button, the window is hidden before it is even clicked.

The effect is as follows

How to use Python+tkinter to write a forced confession artifact

The above is the detailed content of How to use Python+tkinter to write a forced confession artifact. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete