【发布时间】:2021-12-20 15:27:10
【问题描述】:
如何在此代码中获取 tkinter.messagebox 的名称条目? def.OnClick 看不到 myInput 变量。请帮助我,我是 Python 的新手
import tkinter as tk
from tkinter import *
from tkinter import messagebox
from tkinter.font import names
# window settings
win = Tk()
win.geometry("250x350")
win.resizable(0,0)
win.title("Test app")
# def and other
Input1 = StringVar()
def show1():
myLabel3 = Label(win, text="I love Python :)", font=("Montserrat", 18), highlightbackground='darkred', highlightthickness=3)
myLabel3.place(x=30, y=130)
myLabel4 = Label(win, text="Now, enter your name!", font=("Montserrat", 15))
myLabel4.place(x=5, y=190)
myInput = Entry(win, font=("Montserrat", 17), textvariable=Input1, width=100, justify=LEFT)
myInput.place(x=0, y=240)
Button2 = Button(win, text="Apply", font=("Montserrat"), command=onClick)
Button2.place(x=90, y=290)
def onClick():
tk.messagebox.showinfo("Your name", f"Your name - {myInput}")
# widgets
myLabel1 = Label(win, text="Hello World!", font=("Montserrat", 20))
myLabel2 = Label(win, text="Click this button!", font=("Montserrat", 16))
Button1 = Button(win, text="Click me pls!", font=("Montserrat"), command=show1)
# widgets settings
myLabel1.place(x=35)
myLabel2.place(x=26, y=40)
Button1.place(x=62, y=83)
win.mainloop()
【问题讨论】:
-
用
{Input1.get()}替换{myInput} -
请发布并接受答案,以便其他用户知道该问题已得到满意的回答。
标签: python function tkinter tk messagebox