tkinter:将askopenfilename设置为entry

时间:2018-01-07 22:22:04

标签: python tkinter python-2.x

我正在尝试将askopenfilename设置为输入框。但我不知道该怎么做。

3

它给了我这个错误

import Tkinter as tk
from tkFileDialog import *
import ttk


class ESA(tk.Tk):
    def __init__(self,*args,**kwargs):
        tk.Tk.__init__(self,*args,**kwargs)
        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames={}
        for f in (newProject, existingProject, exportProject):
            frame = f(container, self)
            self.frames[f] = frame
            frame.grid(row=0, column=0, sticky = "nsew")

        self.show_frame(newProject)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class newProject(tk.Frame):
    def load_file(self):
        global fname,logo

        self.fname = askopenfilename(initialdir=r"C:\Users",
                                     filetypes=(
                                     ('JPEG / JFIF','*.jpg'),
                                     ('Portable Network Graphics','*.png'),
                                     ('Windows Bitmap','*.bmp'),
                                     ("All files", "*.*")))

        newProject.__init__.clientLogoEntry.delete(0, END)
        newProject.__init__.clientLogoEntry.insert(0, self.fname)

    def __init__(self, parent, controller):

        tk.Frame.__init__(self,parent)

        stepOne = tk.LabelFrame(self, text=" 1. Configuration:")
        stepOne.grid(row=1, columnspan=7, sticky='WE', \
                     padx=5, pady=5, ipadx=5, ipady=5)

        logo = tk.StringVar()
        clientLogoEntry = tk.Entry(stepOne, textvariable=logo)
        clientLogoEntry.grid(row=4, column=1, sticky="WE", pady=2)
        stepOne.button = tk.Button(stepOne, text="Browse...",
                                   command=self.load_file, width=10)
        stepOne.button.grid(row=4, column=7, sticky="W")

1 个答案:

答案 0 :(得分:0)

使用self.clientLogoEntry可以访问类

中的元素
class newProject(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self,parent)

        stepOne = tk.LabelFrame(self, text=" 1. Configuration:")
        stepOne.grid(row=1, columnspan=7, sticky='WE', \
                     padx=5, pady=5, ipadx=5, ipady=5)

        logo = tk.StringVar()

        # use `self`
        self.clientLogoEntry = tk.Entry(stepOne, textvariable=logo)
        self.clientLogoEntry.grid(row=4, column=1, sticky="WE", pady=2)

        stepOne.button = tk.Button(stepOne, text="Browse...",
                                   command=self.load_file, width=10)
        stepOne.button.grid(row=4, column=7, sticky="W")

    def load_file(self):

        self.fname = askopenfilename(initialdir=r"C:\Users",
                                     filetypes=(
                                     ('JPEG / JFIF','*.jpg'),
                                     ('Portable Network Graphics','*.png'),
                                     ('Windows Bitmap','*.bmp'),
                                     ("All files", "*.*")))

        # use `self`
        self.clientLogoEntry.delete(0, END)
        self.clientLogoEntry.insert(0, self.fname)