2013-04-06 100 views
0

我有一个朋友编写的python代码,因为我对Python有很小的了解。 我想将它转换成一个GUI,因为我们将代码作为程序分发给它,使其对初学者友好。将此Python代码转换为可视化GUI?

反正这里是代码:

import time, os, sys 
try: 
if len(sys.argv) < 2: 
    fn = raw_input("Enter the name of the file you want to edit: ") 
else: fn = sys.argv[1] 
f = open(fn) 
b = f.read() 
for i in b[:300]: 
    print hex(ord(i))[2:], 
f.close() 
line = str(0x15c)+'-'+str(0x15f) 
if len(sys.argv)<3: 
    hexcode = raw_input("3 bytes color hex number: ") 
else: hexcode = sys.argv[2] 
if not hexcode.startswith('0x'): 
    hexcode = '0x'+hexcode 
hexstr = '0x' 
start = int(line.split('-')[0]) 
end = int(line.split('-')[1]) 
for i in b[start:end]: 
    hexstr+=hex(ord(i))[2:] 
ascii = '' 
for i in range(2,len(hexcode),2): 
    char = chr(int(hexcode[i:i+2],16)) 
    ascii+=char 
b = b[:start]+ascii+b[end:] 
for i in b[:300]: 
    print hex(ord(i))[2:], 
except Exception, x: 
print x 
time.sleep(3) 
finally: 
f = open(fn,'wb') 
f.write(b) 
f.close() 

现在我发现这一个教程,但不知道如何使用它: #simple GU0I

from Tkinter import * 

root = Tk() 
root.title("BreffHexReplace") 
root.geometry("400x200") 

app = Frame(root) 
app.grid() 
label = Label(app, text = "This is a label!") 

label.grid() 

root.mainloop() 

任何帮助吗? 谢谢!

还有一件事,在这段代码中,我输入名称或者换句话说十六进制后,它给我显示了一个十六进制列表,我怎样才能使它不显示?

谢谢!

回答

0

因为这个程序很简单,所以你可以试试EasyGui(easygui.sourceforge.net/)。要开始,请添加import easygui as eg。该行加载EasyGui模块。接下来,将fn = raw_input("Enter the name of the file you want to edit: ")替换为fn = eg.fileopenbox(title = 'HexReplace', msg = 'Browse to the file you wish to edit')。这将打开一个框以浏览到想要的文件。还用hexcode = eg.enterbox(msg = '3 bytes color hex number', title = 'HexReplace')代替hexcode = raw_input("3 bytes color hex number: ")。这会弹出一个输入框。将print x替换为eg.msgbox(title = 'HexReplace', msg = x)。这显示了一个例外的消息框。参数title是窗口标题。要删除十六进制列表,请在导入和try块之间添加:null = open(os.devnull, 'W's); oldstdout = system.stdout; sys.stdout = null 这将使所有打印消息无效。

+0

我得到了名FN没有定义:(NVM得到了它,我笨, – thethiny 2013-04-06 21:06:42

+0

@thethiny:什么行号都Python的说在 – refi64 2013-04-06 23:20:01

+0

我说NVM它的工作,我忘了,反正加FN = 由于发生了错误? ,,,你能帮忙吗?> http://stackoverflow.com/questions/15856173/python-display-a-hex-color-that-c​​an-be-changed – thethiny 2013-04-06 23:21:02