我不能在程序函数中使用变量

时间:2019-12-16 17:11:15

标签: tkinter

有一个带有GUI的程序,该代码用PyCharm中的python 3.7编写。 问题:您不能在函数crypto()和create_mask()中使用可变程度。 我将使鉴赏家更容易提供帮助,因为我自己的python编程知识为零。 谢谢您的帮助。 程序代码:

import os
from tkinter import
from tkinter import filedialog as fd
from tkinter import messagebox as mb


global degree1


def variable():
    l = encrypt_st.get()
    if I == 1:
        degree1 = 1
    elif I == 2:
        degree1 = 2
    elif I == 3:
        degree1 = 4
    elif I == 4:
        degree1 = 8
    return degree1


def encrypt(degree1):

    f = fd.askopenfilename(title="Select text file")
    text = open(f, 'r')
    d = fd.askopenfilename(title="Select stegocontainer")
    start_bmp = open(d, 'rb')
    encode_bmp = open('encode_bmp', 'wb')

    text_len = os.stat(f).st_size
    img_len = os.stat(d).st_size
    if text_len >= (img_len * degree1 / 8) - 54:
        mb.showerror("It is not possible to hide the data select a larger container")
    return

    first54 = start_bmp.read(54)
    # print(first54) # show the first 54 bytes
    encode_bmp.write(first54)

    text_mask, img_mask = create_masks(degree1)
    while True:
        symbol = text.read(1)
        if not symbol:
            mb.showerror("Can't read the file")
            break
        symbol = ord(symbol)
        for byte_amount in range (0, 8, degree):
            img_byte = int.from_bytes(start_bmp.read(1), sys.byteorder) & img_mask
            bits = symbol & text_mask
            bits >> 8-degree
            img_byte |= bits
            encode_bmp.write(img_byte.to_bytes(1, sys.byteorder))
            symbol <<= degree


def decrypt():
    pass


def create_masks(degree1):
    text_mask = 0b1111111111[enter image description here][1]
    img_mask = 0b1111111111

    text_mask <<= (8 - degree1)
    text_mask %=256
    img_mask >>= degree1
    img_mask <<= degree1
    return text_mask, img_mask


root = Tk()
root.title
root.geometry("400x90")
button1 = Button(text = "Hide data", command=encrypt)
button1.place(x=1, y=1)
lab1 = Label(text="Select degree of coding:")
encrypt_st = IntVar()
encrypt_st.set(0)
var1 = Radiobutton(text="1", value=1, variable=encrypt_st, command=encrypt)
var2 = Radiobutton(text="2", value=2, variable=encrypt_st, command=encrypt)
var3 = Radiobutton(text="4", value=3, variable=encrypt_st, command=encrypt)
var4 = Radiobutton(text="8", value=4, variable=encrypt_st, command=encrypt)
lab1.place(x=100, y=1)
var1.place(x=100, y=20)
var2.place(x=135, y=20)
var3.place(x=170, y=20)
var4.place(x=205, y=20)
root.mainloop()

错误代码:  在 call _ 中的文件“ C:\ Users \ 1 \ AppData \ Local \ Programs \ Python \ Python38-32 \ lib \ tkinter__init __。py”,第1883行     返回self.func(* args) TypeError:encrypt()缺少1个必需的位置参数:'degree1'

由www.DeepL.com/Translator(免费版)翻译

1 个答案:

答案 0 :(得分:0)

两件事:

  1. 在全局范围内使用global无效。当变量在内部范围内(例如函数或类定义)时,应将其声明为global。要解决此问题,应将global degree1添加到使用degree1变量的任何函数中。附带说明一下,global的使用是一种皱眉,因为有更好的方法可以实现“共享”变量的效果。最常见的方法是使用类方法。

  2. tkinter并未提供将事件参数传递给事件命令的现成方法。但是,我可以想到两种解决方法。第一个(可能是最常用的)是使用lambda,它返回函数的句柄。

var1 = Radiobutton(text="1", 
                   value=1, 
                   variable=encrypt_st, 
                   command=lambda: encrypt(degree1))

另一种方法是使用部分函数,​​您可以在使用functools库时创建该函数。

from functools import partial
partial_encrypt = partial(encrypt, degree1)
# partial_encrypt is now a partial object which will call
# encrypt(degree1) when called. We can pass this as a command
# to your tkinter widget.

var1 = Radiobutton(text="1", 
                   value=1, 
                   variable=encrypt_st, 
                   command=partial_encrypt)