来自文本小部件的全局字符串?

时间:2018-04-06 16:30:55

标签: python tkinter global-variables

我是一个新手,正在努力从文本小部件中获取两个简单的字符串输入,并在我的程序中稍后使用它。如何迁移字符串keypadmessage以在其他地方输入?

#Text widget to input 2 text strings and store as 'keypad' and 'message'
from tkinter import *
master = Tk()
master.title("simple one time pad")
master.resizable(0,0)

global keypad
global message

k = Text(master, width=50,height=10)
k.pack()

separator = Frame(height=2, bd=1, relief=SUNKEN)
separator.pack(fill=X, padx=5, pady=5)

m = Text(master, width=50,height=10)
m.pack()

def encrypt():         
    keypad = (k.get(1.0, END))
    str(keypad)
    message = (m.get(1.0, END))
    str(message)

    print(keypad)
    print(message)            

mb = Button(master, text="Encrypt", command=encrypt)
mb.pack(anchor=N)

mainloop()

print(keypad)
print(message)

给出以下错误:

Traceback (most recent call last):
  File "/home/maverick/Window-hell-1.py", line 39, in <module>
    print(keypad)
NameError: name 'keypad' is not defined

我在print()中获得了第一组def encrypt:,但在mainloop()之后崩溃了。 我尝试了整个self.xxx例程,但后来我的小部件无法正常运行。

0 个答案:

没有答案