【问题标题】:Cant Load Image Into Canvas from Local Directory. With help from button and fileMenu无法从本地目录将图像加载到画布中。在按钮和文件菜单的帮助下
【发布时间】:2014-07-30 11:18:11
【问题描述】:

导入 os, os.path, sys 从全局导入全局 从 Tkinter 导入 * 导入 tkFileDialog, Tkconstants 从 tkFileDialog 导入 * 将 tkMessageBox 导入为框 从 PIL 导入图像,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()  

如果 name == 'ma​​in': 主要()

【问题讨论】:

  • 您的格式有问题。有些代码格式正确,有些则没有。
  • “不能”是什么意思?你有错误吗?你得到错误的图像吗?你没有图像吗?
  • 无错误,图像未显示在可滚动画布中@BryanOakley

标签: python image user-interface tkinter


【解决方案1】:

您似乎在initialize 中创建了一个画布,并在self.canvas 中存储了一个引用。稍后,在askopenfilename 中创建另一个画布,覆盖self.canvas 中的引用,然后您忽略在这个新画布上调用 pack、place 或 grid 以使其出现。

【讨论】:

  • 我编辑为只允许一个 self.canvas 引用,仍然无法获得加载图像背后的逻辑
  • @Vampirux:没有看到您的更改,我没有其他建议。
【解决方案2】:

导入 os, os.path, sys 从 Tkinter 导入 * 导入 tkFileDialog, Tkconstants 从 tkFileDialog 导入 * 从 PIL 导入图像,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()  

【讨论】:

  • 这是一个答案,还是您想为该问题添加更多信息?如果它是一个答案,最好能描述你所做的事情,这样我们就不必逐行比较代码。如果不是答案,请编辑您的原始问题。答案部分仅用于答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-14
  • 2022-10-02
  • 2021-11-02
  • 1970-01-01
  • 2013-07-24
  • 2023-03-29
相关资源
最近更新 更多