在Tkin中使用多个输入窗口

2024-10-01 17:32:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建多个输入窗口,但是当我回答完问题时,我得到了一个错误。在

import tkinter
from tkinter import ttk
window2 = False


#--------ACTION 2-----------

def action2():
    window2 = False
    global e4
    Address = e4.get()
    print("The address is", Address)
    global e5
    ConsDate = e5.get()
    print("The date of construction is", ConsDate)
    global e6
    LastRemoDate = e6.get()
    print("The date of the last remodel is", LastRemoDate)


#----------SECOND WINDOW---------

def window2():
    window2 = tkinter.Tk() 
    window2.title("Name Software")

    TitleLabel = ttk.Label(window2, text = "Name Information")
    TitleLabel.grid(row = 0, column = 1)

    L4 = ttk.Label(window2, text = "What is your address?")
    L4.grid(row =1, column = 0)

    e4 = ttk.Entry(window2, width = 50)
    e4.grid(row = 1, column = 1)

    L5 = ttk.Label(window2, text = "What is the original date of construction?")
    L5.grid(row = 2, column = 0)

    e5 = ttk.Entry(window2, width = 50)
    e5.grid(row = 2, column = 1)

    L6 = ttk.Label(window2, text = "What is the date of the last remodel?")
    L6.grid(row = 3, column = 0)

    e6 = ttk.Entry(window2, width = 50)
    e6.grid(row = 3, column = 1)


    btn = ttk.Button(window2, text = "Submit Answers", command = action2)
    btn.grid(row = 5, column = 1)



window2()

当我运行这个时,它会给我一个错误

^{pr2}$

我怎么解决这个问题?我从剧本的另一部分抄了下来,改了问题。第一部分可行,但这不行

感谢任何帮助。在


Tags: ofthetextdateistkintercolumnlabel
2条回答

您需要将global e4,e5,e6添加到windows2()函数的顶部,以便在函数中使用全局变量(“action2()”正在访问),而不是使用它当前使用的local变量。在

import tkinter
from tkinter import ttk
window2 = False

#     SECOND WINDOW    -

def window2():
    window2 = tkinter.Tk() 
    window2.title("Name Software")

    global e4,e5,e6

    TitleLabel = ttk.Label(window2, text = "Name Information")
    TitleLabel.grid(row = 0, column = 1)

    L4 = ttk.Label(window2, text = "What is your address?")
    L4.grid(row =1, column = 0)

    e4 = ttk.Entry(window2, width = 50)
    e4.grid(row = 1, column = 1)

    L5 = ttk.Label(window2, text = "What is the original date of construction?")
    L5.grid(row = 2, column = 0)

    e5 = ttk.Entry(window2, width = 50)
    e5.grid(row = 2, column = 1)

    L6 = ttk.Label(window2, text = "What is the date of the last remodel?")
    L6.grid(row = 3, column = 0)

    e6 = ttk.Entry(window2, width = 50)
    e6.grid(row = 3, column = 1)


    btn = ttk.Button(window2, text = "Submit Answers", command = action2)
    btn.grid(row = 5, column = 1)

#    ACTION 2     -

def action2():
    window2 = False
    global e4
    Address = e4.get()
    print("The address is", Address)
    global e5
    ConsDate = e5.get()
    print("The date of construction is", ConsDate)
    global e6
    LastRemoDate = e6.get()
    print("The date of the last remodel is", LastRemoDate)

window2()

你必须使两个函数的变量都是全局的。尝试:

import tkinter
from tkinter import ttk
window2 = False
#global e4,e5,e6
#     SECOND WINDOW    -

def window2():
    global e4,e5,e6
    window2 = tkinter.Tk() 
    window2.title("Name Software")

    TitleLabel = ttk.Label(window2, text = "Name Information")
    TitleLabel.grid(row = 0, column = 1)

    L4 = ttk.Label(window2, text = "What is your address?")
    L4.grid(row =1, column = 0)

    e4 = ttk.Entry(window2, width = 50)
    e4.grid(row = 1, column = 1)

    L5 = ttk.Label(window2, text = "What is the original date of construction?")
    L5.grid(row = 2, column = 0)

    e5 = ttk.Entry(window2, width = 50)
    e5.grid(row = 2, column = 1)

    L6 = ttk.Label(window2, text = "What is the date of the last remodel?")
    L6.grid(row = 3, column = 0)

    e6 = ttk.Entry(window2, width = 50)
    e6.grid(row = 3, column = 1)


    btn = ttk.Button(window2, text = "Submit Answers", command = action2)
    btn.grid(row = 5, column = 1)

#    ACTION 2     -

def action2():
    global e4,e5,e6
    window2 = False
    Address = e4.get()
    print("The address is", Address)
    ConsDate = e5.get()
    print("The date of construction is", ConsDate)
    LastRemoDate = e6.get()
    print("The date of the last remodel is", LastRemoDate)

window2()

相关问题 更多 >

    热门问题