import os,os.path,sys 来自glob import glob 来自Tkinter进口* import tkFileDialog,Tkconstants 来自tkFileDialog导入* 将tkMessageBox导入为框 来自PIL导入Image,ImageTk filetext ='选择图像文件'
类示例(框架):
def __init__(self, parent):
Frame.__init__(self, parent, background="grey")
self.parent = parent
menubar=Menu(self.parent)
self.parent.config(menu=menubar)
fileMenu = Menu(menubar, tearoff=0)
fileMenu.add_command(label="Open File", command=self.askopenfilename)
fileMenu.add_command(label="Exit", command=self.onExit)
menubar.add_cascade(label="File", menu=fileMenu)
#self.pack(fill=BOTH, expand=1)
self.initialize()
self.centerWindow()
self.quit()
def onExit(self):
self.quit()
def initialize(self):
self.parent.title("PLAIN-HIDE")
self.pack(fill=BOTH, expand=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(3, pad=7)
self.rowconfigure(0, pad=7)
self.rowconfigure(1, pad=7)
self.rowconfigure(2, pad=7)
self.rowconfigure(3, pad=7)
self.rowconfigure(4, pad=7)
lab=Label(self, text = 'CHOOSE SOURCE IMAGE')
lab.grid(row=0, columnspan=8, pady=4,padx=5)
self.entryVariable = StringVar(None)
self.entry = Entry(self, textvariable=self.entryVariable)
self.entry.grid(row=1, column=1,sticky='EW', pady=4,padx=5)
self.entry.focus_set()
self.entry.config(state='disable')
button = Button(self, text='Browse File..', command=self.askopenfilename)
button.grid(column=1,row=1, sticky='E',pady=4,padx=5)
self.canvas = Canvas(self,width=400, height=300, bg='white')
self.canvas.grid(row=2, column=1, sticky='EW',padx=4,pady=5)
lab2=Label(self, text = 'TYPE YOUR SECRET MESSAGE BELOW')
lab2.grid(row=3, sticky='N',columnspan=8, pady=4,padx=5)
self.entryVariable2 = StringVar(None)
self.entry2 = Entry(self, textvariable=self.entryVariable2)
self.entry2.grid(row=4, column=1,sticky='EW', pady=4,padx=5)
self.entry2.focus_set()
self.entry2.config(state='normal')
var = IntVar(None)
cb = Checkbutton(self, text='Select To Encrypt Message', state='active', variable=var,onvalue = 1, offvalue = 0)
#cb = Checkbutton(self, text='Yes', state='active', variable=var,onvalue = 1, offvalue = 0)
cb.grid(row=5,column=1, sticky='E',padx=4,pady=5)
encode_button = Button(self, text='ENCODE')
encode_button.grid(column=1,row=6, sticky='SE',pady=4,padx=5)
decode_button = Button(self, text='DECODE')
decode_button.grid(column=1,row=6, sticky='SW',pady=4,padx=5)
def askopenfilename(self):
self.filename= tkFileDialog.askopenfilename(initialdir='C:/..',title='Select File' , filetypes=[('Bitmap Images', '*.bmp'),('Png Images', '*.png'),('Gif Images', '*.gif'),("Jpeg","*.jpg"),("All files", "*")])
self.entryVariable.set(self.filename)
if self.filename:
return open(self.filename,'r')
self.entry.update(self.filename)
self.image = Image.open(self.filename)
self.img = ImageTk.PhotoImage(self.image)
self.label1 = Label(self, image=self.img)
self.label1.grid(row=2, column=1)
self.canvas.config(width=self.image.width(), height=self.image.height())
self.canvas.create_image(10, 10, anchor=NW, image=self.img)
self.canvas.img = self.img
self.scrollbar_vert = Scrollbar(self.canvas)
self.scrollbar_hor = Scrollbar(self.canvas)
self.scrollbar_vert.config(orient=VERTICAL)
self.scrollbar_vert.pack(side=RIGHT, fill=Y)
self.scrollbar_hor.config(orient=HORIZONTAL)
self.scrollbar_hor.pack(side=BOTTOM, fill=X)
self.scrollbar_vert.config(command=self.canvas.yview)
self.scrollbar_hor.config(command=self.canvas.xview)
self.canvas.config(yscrollcommand=self.scrollbar_vert.set)
self.canvas.config(xscrollcommand=self.scrollbar_hor.set)
self.canvas.config(scrollregion=self.canvas.bbox(ALL))
self.canvas.pack()
def onClick(self,event):
return 'Text Will Be Encrypted',self.var.get()
def centerWindow(self):
w = 650
h = 550
sw = self.parent.winfo_screenwidth()
sh = self.parent.winfo_screenheight()
x = (sw - w)/2
y = (sh - h)/2
self.parent.geometry('%dx%d+%d+%d' % (w, h, x, y))
def main():
root = Tk()
ex = Example(root)
root.mainloop()
如果名称 =='主要': main()
答案 0 :(得分:0)
您似乎在initialize
中创建了一个画布,并在self.canvas
中存储了一个引用。稍后,在askopenfilename
中,您创建另一个画布,覆盖self.canvas
中的引用,然后忽略在此新画布上调用pack,place或grid以使其显示。
答案 1 :(得分:-1)
import os,os.path,sys 来自Tkinter进口* import tkFileDialog,Tkconstants 来自tkFileDialog导入* 来自PIL导入Image,ImageTk
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent, background="blue")
self.parent = parent
menubar=Menu(self.parent)
self.parent.config(menu=menubar)
fileMenu = Menu(menubar, tearoff=0)
fileMenu.add_command(label="Open File", command=self.askopenfilename)
fileMenu.add_command(label="Exit", command=self.onExit)
menubar.add_cascade(label="File", menu=fileMenu)
#self.pack(fill=BOTH, expand=1)
self.initialize()
self.centerWindow()
self.quit()
def onExit(self):
self.quit()
def initialize(self):
self.parent.title("PLAIN-HIDE")
self.pack(fill=BOTH, expand=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(2,pad=7)
self.rowconfigure(0, pad=7)
self.rowconfigure(1, pad=7)
self.rowconfigure(2,weight=1 ,pad=7)
self.rowconfigure(3, pad=7)
self.rowconfigure(4, pad=7)
self.rowconfigure(5,weight=1, pad=7)
self.lab=Label(self, text = 'CHOOSE SOURCE IMAGE')
self.lab.grid(row=0, columnspan=8, pady=4,padx=5)
self.entryVariable = StringVar(None)
self.entry = Entry(self, textvariable=self.entryVariable)
self.entry.grid(row=1, column=1,sticky='EW', pady=4,padx=5)
self.entry.focus_set()
self.entry.config(state='disable')
button = Button(self, text='Browse File..', command=self.askopenfilename)
button.grid(column=1,row=1, sticky='E',pady=4,padx=5)
self.canvas = Canvas(self,width=500, height=400, bg='white')
self.canvas.grid(row=2, column=1, sticky='NSEW',padx=4,pady=5)
lab2=Label(self, text = 'TYPE YOUR SECRET MESSAGE BELOW')
lab2.grid(row=3, sticky='N',columnspan=8, pady=4,padx=5)
self.area = Text(self, width=5, height=5)
self.area.grid(row=4, column=1, columnspan=1, rowspan=1, padx=5, sticky=E+W+S+N)
self.area.focus_set()
self.area.config(state='normal')
var = IntVar(None)
cb = Checkbutton(self, text='Select To Encrypt Message', state='active', variable=var,onvalue = 1, offvalue = 0)
#cb = Checkbutton(self, text='Yes', state='active', variable=var,onvalue = 1, offvalue = 0)
cb.grid(row=5,column=1, sticky='E',padx=4,pady=5)
encode_button = Button(self, text='ENCODE')
encode_button.grid(column=1,row=6, sticky='SE',pady=4,padx=5)
decode_button = Button(self, text='DECODE')
decode_button.grid(column=1,row=6, sticky='SW',pady=4,padx=5)
def askopenfilename(self):
filename= tkFileDialog.askopenfilename(initialdir='C:/..',title='Select File' , filetypes=[('Bitmap Images', '*.bmp'),('Png Images', '*.png'),('Gif Images', '*.gif'),("Jpeg","*.jpg"),("All files", "*")])
self.entryVariable.set(filename)
if filename == None:
return open(filename,'rb')
self.entry.update(filename)
self.image = Image.open(filename)
self.img = ImageTk.PhotoImage(self.image)
self.canvas.config(width=400, height=300)
self.lab.img = self.img
self.lab.pack()
self.canvas.create_image(1,1, anchor=NW, image=self.img)
self.scrollbar_vert = Scrollbar(self.canvas)
self.scrollbar_vert.pack(side=RIGHT, fill=Y)
self.scrollbar_hor = Scrollbar(self.canvas)
self.scrollbar_hor.config(orient=HORIZONTAL)
self.scrollbar_hor.pack(side=BOTTOM, fill=X)
self.canvas.config(yscrollcommand=self.scrollbar_vert.set)
self.canvas.config(xscrollcommand=self.scrollbar_hor.set)
self.canvas.config(scrollregion=self.canvas.bbox(ALL))
self.scrollbar_vert.config(command=self.canvas.yview)
self.scrollbar_hor.config(command=self.canvas.xview)
def centerWindow(self):
w = 650
h = 550
sw = self.parent.winfo_screenwidth()
sh = self.parent.winfo_screenheight()
x = (sw - w)/2
y = (sh - h)/2
self.parent.geometry('%dx%d+%d+%d' % (w, h, x, y))
def main():
root = Tk()
ex = Example(root)
root.mainloop()
if __name__ == '__main__':
main()