-1

I just wrote a program in Python. The program does what follows:

There is a class called original. It has 2 methods (a part of init). The first method is code. What "code" does is receive a string and encode it changing every character from the received string for another character of the ASCII table. This ASCII character depends of the function generacion_clave. The function generacion_clave generates randomly a number between 1 and k (k is an input parameter for this function).

For example the character "e" is in the place 101 on the ASCII table. So if the generated number is 8, the character e is changed for the character in the ASCII table which has the position 109 (101 + 8)

The method decode receives the encoded string and the clave, and it returns the decoded string.

What I would like to do is add to the program a graphic interface that contains:

An entry box for the original String, A Canvas (or a output box?) where the encoded and decoded string will we shown, a button to encode and a button to decode.

I am quite new in Python, I have read some tutorials about Tkinter but I don´t find it easy at all. So I would appreciate any help :)

from random import randint 

class original():

def __init__(self, mensaje_original):
    self.mensaje_original = mensaje_original


def code(self, gen_clav, *args):
    cadena_codificada ="" 
    clave = gen_clav(*args) 
    for i in self.mensaje_original:
        clave_mod = ord(i) + clave
        if clave_mod > 255:
            clave_mod = clave_mod - 255

        cadena_codificada = cadena_codificada + chr(clave_mod)

    return cadena_codificada, clave    

def decode(self, cadena_cod_clave):
    cadena_decodificada =""
    clave = int(cadena_cod_clave[1])
    for i in cadena_cod_clave[0]:
        clave_mod = ord(i) - clave
        if clave_mod > 255:
            clave_mod = clave_mod - 255
        cadena_decodificada = cadena_decodificada + chr(clave_mod)          

    return cadena_decodificada, cadena_cod_clave[1]     

def generacion_clave(k):
cl = randint(1, k + 1)
return cl


mensaje_original = "Hola tio como estas"   
mensaje = original(mensaje_original)
cad_cod_clav = mensaje.code(generacion_clave, 10)


cad_dec_clav = mensaje.decode(cad_cod_clav)
print "La cadena original es: %s" %cad_dec_clav[0]   
print "La cadena cifrada es: %s" %cad_cod_clav[0]
print "la clave es: %d " %cad_cod_clav[1]

Thanks a lot in advance! Pablo

4

2 回答 2

1

用 Tk 构建 GUI 并不难。

您将需要一个条目(用于输入的小部件)、一组标签(用于打印输出)和一个按钮。

这里提到了构建主框架和使用入口小部件。http://www.effbot.org/tkinterbook/entry.htm

我建议你试试基本的 gui(也许在一个单独的文件中),我们会在 GUI 完成后链接它们。

如果您需要,我将在创建基本 GUI 后帮助您进行链接。

于 2015-04-15T13:40:47.500 回答
-2

我试着去做。我开始只添加 2 个按钮(编码和退出)。Encode 应该调用类 original 中的方法 encode 并且 Quit 应该关闭窗口(有效)。有人知道为什么“编码”按钮不起作用吗?

from random import randint 
from Tkinter import *


class original():

def __init__(self, mensaje_original):
    self.mensaje_original = mensaje_original


def code(self, gen_clav, *args):
    cadena_codificada ="" 
    clave = gen_clav(*args) 
    for i in self.mensaje_original:
        clave_mod = ord(i) + clave
        if clave_mod > 255:
            clave_mod = clave_mod - 255

        cadena_codificada = cadena_codificada + chr(clave_mod)
    print  cadena_codificada, clave   
    return cadena_codificada, clave    

def decode(self, cadena_cod_clave):
    cadena_decodificada =""
    clave = int(cadena_cod_clave[1])
    for i in cadena_cod_clave[0]:
        clave_mod = ord(i) - clave
        if clave_mod > 255:
            clave_mod = clave_mod - 255
        cadena_decodificada = cadena_decodificada + chr(clave_mod)          

    return cadena_decodificada, cadena_cod_clave[1]     

def generacion_clave(k):
    cl = randint(1, k + 1)
    return cl


class graficos:

    def __init__(self, master):
    frame = Frame(master)
    frame.pack()
    self.encode_buttom = Button(frame, text = "Encode!", command = self.printMessage)
    self.encode_buttom.pack(side = LEFT)

    self.quit_buttom = Button(frame, text = "Quit", command = frame.quit)
    self.quit_buttom.pack(side = LEFT)

def printMessage(self):
    mensaje_original = "Hola tio como estas"   
    mensaje = self.original(mensaje_original)
    print mensaje



root = Tk()
root.geometry("250x250")

b = graficos(root)
root.mainloop()

非常感谢。问候巴勃罗

于 2015-04-16T09:05:03.930 回答