python - 如何将输入字段值传递给函数
问题描述
我正在尝试使用tkinter
on制作一个基于 GUI 的加密程序python3
。这里的一个选项是加密图像,现在有一个输入字段,用户将在其中输入模式,1 表示 RGB,0 表示灰度。现在我正在尝试将该输入字段值传递给加密和解密函数,该函数将在按下相应按钮时执行。但是在将值传递给函数时出现错误。编写按钮和输入字段的脚本,如下:
global mode
def getvalue():
mode =int(mode_txt.get())
return mode
image_window = Tk()
image_window.geometry ( '350x200' )
image_window.title ('Image Cryptography')
lbl1 = Label(image_window, text = "Select mode(0 for greyscale/ 1 for RGB): " ).place( x=20, y=40 )
mode_txt = IntVar
mode_txt = Entry(image_window, width = 4) #the entry field
mode_txt.place(x= 300, y=40)
mode_txt.bind('<Return>', getvalue)
mode_txt.pack()
现在我通过mode
这样的:
def encrypt():
mode = getvalue()
if(mode == 0): #greyscale
#some code#
按钮是这样的:
btn_decrypt = Button( image_window, text = "Image Decryption", command = decrypt)
btn_decrypt.place( x=20, y=100 )
btn_encrypt = Button( image_window, text = "Image Encryption", command = encrypt)
btn_encrypt.place( x=200, y=100 )
btn_exit = Button( image_window, text = "Go Back" , command = goback).place( x=140, y=150 )
image_window.mainloop()
我得到的错误是:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib64/python3.7/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "image1.py", line 98, in decrypt
if(mode == 0):
NameError: name 'mode' is not defined
我不知道我哪里出错了?任何帮助将不胜感激。谢谢你。
解决方案
推荐阅读
- python - 一行来检查 2 个数据框中的列名是否包含字符串?
- angular - 如何通过角度的viewchild正确设置输入?
- scala - 结构化火花流左外连接的行为类似于内连接
- javascript - 当用户标记不和谐机器人时,我该如何做到这一点,它会回复一条消息?
- sql - 查询如何根据主键选择列中的最高字母?
- c++ - 从复制赋值重载中通过引用返回
- azure - 天蓝色上的 terraform - 创建具有私有连接的密钥库
- python - QuantLib Python Hull White 模型 - RuntimeError:时间 (20) 已超过最大曲线时间 (19)
- c++ - 字符串到C++中的char数组
- typescript - 用打字稿反应导航getparam