acid-lemon 发表于 2022-10-6 12:12:29

获取子窗口输入框内容

本帖最后由 acid-lemon 于 2022-10-6 12:15 编辑

部分代码如下:


def card_write():
    card_title = e2.get() #问题出在这句
    e1.insert(0, """<local:MyCard Title="%s">"""% card_title)

def card(event):
    top = ttk.Toplevel()
    top.title("创建卡片")
    w = 500
    h = 307
    x = (root.winfo_screenwidth() - w) / 2
    y = (root.winfo_screenheight() - h) / 2
    top.geometry("%dx%d+%d+%d" % (w, h, x, y))
    L1 = ttk.Label(top, text="标题:", font = ("微软雅黑",13))
    e2 = ttk.Entry(top)
    b2 = ttk.Button(top, text="确定", command = card_write)
    b3 = ttk.Button(top,text = "取消", command = top.destroy)
    L1.place(width = 60, height = 30, x = 20, y = 15)
    e2.place(width = 400, height = 30, x = 70, y = 15)
    b2.place(width=60, height=30, x=100, y=200)
    b3.place(width=60, height=30, x=340, y=200)


在父窗口按下按钮,打开子窗口,现在我想在父窗口读取e2的内容
但是e2是个局部变量,不能使用,求解决方法

Twilight6 发表于 2022-10-6 12:21:50



那将 e2 设置声明为全局就行了吧

在 card 方法添加一行代码:global e2

lassiter 发表于 2022-10-6 12:35:02

声明全局变量,在def card_write():上边,也就是第一行,声明global e2

acid-lemon 发表于 2022-10-6 12:35:50

Twilight6 发表于 2022-10-6 12:21
那将 e2 设置声明为全局就行了吧

在 card 方法添加一行代码:global e2

谢了
页: [1]
查看完整版本: 获取子窗口输入框内容