how to Entry.insert() work with frames in tkinter?

别说谁变了你拦得住时间么 提交于 2019-12-11 19:12:09

问题


i am trying to make a program that help me in my work at the school and i made lots of progress with it but i am messing now i need to fined how to make the path of the file to show on the Entry widget i tried lots of the stuffs in here and i may didn't git the idea behind it please help me with it

import tkinter as tk 
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showerror
from tkinter import ttk
import xlrd

class SchoolProjict(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.app_data = {"name": tk.StringVar(),
                         }
        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 (StartPage,  SetingPage):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row = 0, column = 0, sticky = "nsew")
        self.show_frame(StartPage)

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

    def get_page(self, classname):
        for page in self.frames.values():
            if str(page.__class__.__name__) == classname:
                return page
        return None

def printingstuff(var1):
    print (var1)

def printontherthing(page_class):
    print(page_class)


class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        self.controller = controller
        tk.Frame.__init__(self, parent)
        lablel = ttk.Label(self, text = "Main Page")
        lablel.pack(pady = 10, padx = 10)
        button2 = ttk.Button(self, text = "Siting", command = lambda: controller.show_frame(SetingPage))
        button2.pack()

class SetingPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        lablel = tk.Label(self, text = "Siting Page")
        lablel.grid(row = 0, column = 0)
        text1 = tk.Entry(self)  #<== i want to show the path of the file i am going to open Here after i select it from openfile 
        text1.grid(row = 2, column = 0)
        text1.focus()
        button1 = tk.Button(self, text = "print text1", command = lambda: printingstuff(text1.get()))
        button1.grid(row = 3, column = 0)
        button2 = tk.Button(self, text="open file", command= self.load_file, width=10)
        button2.grid(row = 3, column = 1)
        button4 = tk.Button(self, text = "Main Page", command = lambda: controller.show_frame(StartPage))
        button4.grid(row = 4, column = 1)

    def load_file(self):
        fname = askopenfilename(filetypes=(("Excel file", "*.xls"),
                                           ("HTML files", "*.html;*.htm"),
                                           ("All files", "*.*") ))
        if fname:
            try:
                # print(fname)
                value = str(fname)
                page_var = self.controller.get_page("SetingPage")
                page_var.text1.insert(0, value)
                return

            except:                    
                showerror("Open Source File", "Failed to read file\n'%s'" % fname)
            return

app = SchoolProjict()
app.mainloop()

it gave me an error whine i run it and select the file this is what i have as an error


回答1:


You need to make your text an attribute of the class SetingPage by adding self.

class SetingPage(tk.Frame):
    def __init__(self, parent, controller):
        ...
        self.text1 = tk.Entry(self)  #<== i want to show the path of the file i am going to open Here after i select it from openfile
        self.text1.grid(row = 2, column = 0)
        self.text1.focus()
        button1 = tk.Button(self, text = "print text1", command = lambda: printingstuff(self.text1.get()))
        ...


来源:https://stackoverflow.com/questions/56386715/how-to-entry-insert-work-with-frames-in-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!