Python 3.x 我的函数中无法识别tkinter中的小部件

Python 3.x 我的函数中无法识别tkinter中的小部件,python-3.x,tkinter,Python 3.x,Tkinter,我正在制作一个Caesar密码GUI,我正在努力使我的数据输入和输出在我的函数中工作。当我尝试将.get或.insert与文本小部件一起使用时,会出现以下错误: Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\sethb\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", lin

我正在制作一个Caesar密码GUI,我正在努力使我的数据输入和输出在我的函数中工作。当我尝试将.get或.insert与文本小部件一起使用时,会出现以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\sethb\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:/Users/sethb/PycharmProjects/TextCipher/Cipher 2.0.py", line 52, in <lambda>
    cipher = ttk.Button(self, text='Cipher', command=lambda: clickedCipher())
  File "C:/Users/sethb/PycharmProjects/TextCipher/Cipher 2.0.py", line 111, in clickedCipher
    string = inserttxt.get()
NameError: name 'inserttxt' is not defined.  
Tkinter回调中出现异常 回溯(最近一次呼叫最后一次): 文件“C:\Users\sethb\AppData\Local\Programs\Python\Python38-32\lib\tkinter\\ uuuuuuu init\uuuuuuuuu.py”,第1883行,在调用中__ 返回self.func(*args) 文件“C:/Users/sethb/PycharmProjects/textchipher/Cipher 2.0.py”,第52行,在 cipher=ttk.Button(self,text='cipher',command=lambda:clickedCipher()) clickedCipher中的文件“C:/Users/sethb/PycharmProjects/TextCipher/Cipher 2.0.py”,第111行 string=inserttxt.get() NameError:未定义名称“inserttxt”。 我还没有找到解决这个问题的办法。如果有人知道我做错了什么,我会很感激的。这是我最不需要开始工作的事情

import tkinter as tk
from tkinter import messagebox
from tkinter import ttk

class tkinterApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (mainWindow, newWindow):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(mainWindow)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class mainWindow(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        lbl = ttk.Label(self,
                text="Welcome to my Caesar Cipher.  To begin the cipher                 
   press start.\nTo learn more about"
                        " the Caesar Cipher press facts.")
        lbl.grid(column=0, row=0)

        factsbtn = ttk.Button(self, text="Facts", command=clickedFacts)
        factsbtn.grid(row=2, column=1, padx=10, pady=10)

        startbtn = ttk.Button(self, text="Start", command=lambda:     
        controller.show_frame(newWindow))
        startbtn.grid(row=1, column=1, padx=10, pady=10)

   class newWindow(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        enterlbl = ttk.Label(self, text="Enter your text below and click 
Cipher to run the cipher program")
        enterlbl.grid(row=0)

        inserttxt = tk.Text(self)
        inserttxt.insert(tk.END, 'enter your message here')
        inserttxt.grid(row=1)

        cipher = ttk.Button(self, text='Cipher', command=lambda: 
clickedCipher())
        cipher.grid(row=2)

        outputlbl = ttk.Label(self, text="The ciphered text is:")
        outputlbl.grid(row=3)

        ntxtbox = tk.Text(self)
        ntxtbox.grid(row=4)

def clickedFacts():
    History='    The Caesar Cipher is an encryption method that dates back 
to the 1st century B.C.  It is named' \
        ' after Julias Caesar who used it to send military messages across 
his empire.  It is believed that when' \
        ' his enemies found it, they assumed the messages were written in 
another language and simply did not try' \
        ' to decipher it.\n \n    My cipher uses an 8 letter shift, 
meaning an A will be written as an I.  Julius' \
        ' Caesar is believed to have used a shift of 3, while his nephew 
used a shift of only 1'
    messagebox.showinfo('Facts', History)

def split(word):
    return [char for char in word]

def shift(indexes):

    if indexes < 25 - 8:
        indexes += 8
    else:
        indexes -= 17
    return indexes

def intoCipher(otext, p, k):
    arrLow = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    arrCap = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
    indexA = 0
    lowOrUp = '0'
    b = 0
    x = len(otext)
    index = 0
    while index < x:
        while indexA < 25 and lowOrUp == '0':
            if otext[index] == arrLow[indexA]:
                lowOrUp = '1'
                p = indexA
                b = 1
            elif otext[index] == arrCap[indexA] and lowOrUp == '0':
                lowOrUp = '1'
                p = indexA
                b = 2
            indexA += 1
        k = shift(p)
        if b == 1:
            otext[index] = arrLow[k]
        elif b == 2:
            otext[index] = arrCap[k]
        b = 0
        lowOrUp = '0'
        index += 1
        indexA = 0
    return otext

def clickedCipher():
   string = global inserttxt.get()
    string = str(string)
    string = intoCipher(string, 0, 0)
    string = ''.join(string)
    global ntxtbox.insert(0, string)

app = tkinterApp()
app.mainloop()
将tkinter作为tk导入
从tkinter导入消息框
从tkinter导入ttk
tkinterApp类(tk.tk):
定义初始化(self,*args,**kwargs):
tk.tk.\uuuuu初始化(self,*args,**kwargs)
容器=tk.框架(自身)
container.pack(side=“top”,fill=“both”,expand=True)
container.grid_rowconfigure(0,权重=1)
container.grid\u column配置(0,权重=1)
self.frames={}
对于F in(主窗口、新窗口):
框架=F(容器,自身)
self.frames[F]=帧
frame.grid(行=0,列=0,sticky=“nsew”)
自我显示框架(主窗口)
def显示画面(自身,续):
帧=自身帧[续]
frame.tkraise()
类主窗口(tk.Frame):
定义初始化(自、父、控制器):
tk.Frame.\uuuu init\uuuuu(自,父)
lbl=ttk.标签(自,
text=“欢迎使用我的凯撒密码。开始密码
按开始。\n要了解有关“的详细信息”
“凯撒密码出版社的事实。”)
lbl.grid(列=0,行=0)
factsbtn=ttk.按钮(self,text=“Facts”,command=clickedFacts)
事实网格(行=2,列=1,padx=10,pady=10)
startbtn=ttk.按钮(self,text=“Start”,command=lambda:
控制器。显示_帧(新窗口))
起始网格(行=1,列=1,padx=10,pady=10)
类newWindow(tk.Frame):
定义初始化(自、父、控制器):
tk.Frame.\uuuu init\uuuuu(自,父)
enterlbl=ttk.Label(self,text=“在下面输入文本并单击
运行密码程序的密码)
enterlbl.grid(行=0)
inserttxt=tk.Text(self)
inserttxt.insert(tk.END,“在此处输入消息”)
inserttxt.grid(行=1)
cipher=ttk.Button(self,text='cipher',command=lambda:
clickedCipher())
cipher.grid(行=2)
outputlbl=ttk.Label(self,text=“加密文本为:”)
outputlbl.grid(行=3)
ntxtbox=tk.Text(self)
ntxtbox.grid(行=4)
def clickedFacts():
History='凯撒密码是一种可以追溯到以前的加密方法
直到公元前1世纪,它被命名为'\
“在朱利亚斯·凯撒(Julias Caesar)用它向全世界发送军事信息之后
他的帝国。人们相信当\
他的敌人发现了它,他们认为这些信息是用英文写的
另一种语言,只是没有尝试'\
'来破译它。\n\n我的密码使用8个字母的移位,
意思是A将被写成I.朱利叶斯的\
据信,凯撒的班次是3班,而他的侄子
只使用了1’的移位
messagebox.showinfo('Facts',History)
def拆分(word):
return[char for char in word]
def班次(索引):
如果指数<25-8:
索引+=8
其他:
索引-=17
返回索引
def输入密码(otext、p、k):
arrLow=['a','b','c','d','e','f','g','h','i','j','k','l',',
“m”、“n”、“o”、“p”、“q”、“r”、“s”、“t”、“u”、“v”、“w”、“x”、“y”、“z”]
arrCap=['A','B','C','D','E','F','G','H','I','J','K','L',
“M”、“N”、“O”、“P”、“Q”、“R”、“S”、“T”、“U”、“V”、“W”、“X”、“Y”、“Z”]
指数=0
lowOrUp='0'
b=0
x=len(otext)
索引=0
当指数
inserttxt
是一个局部变量。要使其成为全局的,必须在创建时将其声明为全局的

但是,由于您使用的是类,所以最好将其设置为实例变量。然后,您应该在类上创建
clickedchipher
方法,这样该方法就可以轻松访问变量,而不会污染全局名称空间

它看起来像这样:

class newWindow(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        ...
        self.inserttxt = tk.Text(self)
        self.ntxtbox = tk.Text(self)
        cipher = ttk.Button(self, text='Cipher', command=self.clickedCipher)
        ...

    def clickedCipher(self):
        string = self.inserttxt.get()
        string = str(string)
        string = intoCipher(string, 0, 0)
        string = ''.join(string)
        self.ntxtbox.insert(0, string)

inserttxt
是一个局部变量。要使其成为全局的,必须在创建时将其声明为全局的

但是,由于您使用的是类,所以最好将其设置为实例变量。然后,您应该在类上创建
clickedchipher
方法,这样该方法就可以轻松访问变量,而不会污染全局名称空间

它看起来像这样:

class newWindow(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        ...
        self.inserttxt = tk.Text(self)
        self.ntxtbox = tk.Text(self)
        cipher = ttk.Button(self, text='Cipher', command=self.clickedCipher)
        ...

    def clickedCipher(self):
        string = self.inserttxt.get()
        string = str(string)
        string = intoCipher(string, 0, 0)
        string = ''.join(string)
        self.ntxtbox.insert(0, string)

请发布完整的错误代码?我输入了错误代码和代码的其余部分。如果您没有输入错误代码,则不必使用
lambda