我正在尝试创建多个输入窗口,但是当我回答完问题时,出现错误。
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()
当我运行它时,它给了我错误
Line 12, in action2
Address = e4.get()
NameError: name 'e4' is not defined
该如何解决?我从脚本的其他部分复制了它并更改了问题。第一部分有效,但无效。
感谢您的帮助。
答案 0 :(得分:2)
必须在两个函数中使变量成为全局变量。试试:
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()
答案 1 :(得分:0)
您需要将global e4,e5,e6
添加到windows2()
函数的顶部,以便在该函数中使用全局变量(“ action2()”正在访问),而不是使用局部变量。当前正在使用。
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()