0

i'm trying to make (text1) the Entry to get the file path that i open to show up what i mean whin i open a file it show me the file path on the entry(textbox) ps: sorry for the bad example and English

''''python

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

here is my class for the fream

class SchoolProjict(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 (StartPage, PageOne, 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()

this on to test printing from entry

def printingstuff(var1):

    print (var1)

this to open a file i want it to chang the entry to show the file path

def load_file():
        fname = askopenfilename(filetypes=(("Excel file", "*.xls"),
                                           ("HTML files", "*.html;*.htm"),
                                           ("All files", "*.*") ))
        if fname:
            try:
                print(fname)

                return

            except:                     # <- naked except is a bad idea
                showerror("Open Source File", "Failed to read file\n'%s'" % fname)
            return

here are the frames for the program

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

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        lablel = tk.Label(self, text = "Main Menu")
        lablel.pack(pady = 10, padx = 10)
        button1 = tk.Button(self, text = "Start Page", command = lambda: controller.show_frame(StartPage))
        button1.pack()
        button2 = tk.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)
        lablel = tk.Label(self, text = "Siting Page")
        lablel.pack(pady = 10, padx = 10)
        text1 = ttk.Entry(self)
        text1.pack()
        text1.focus()
        button1 = tk.Button(self, text = "Print from Entry", command = lambda: printingstuff(text1.get()))
        button1.pack()
        button2 = tk.Button(self, text="open File", command= load_file, width=10)
        button2.pack()
        button3 = tk.Button(self, text = "Main Menu", command = lambda: controller.show_frame(PageOne))
        button3.pack()
        button4 = tk.Button(self, text = "Start Page", command = lambda: controller.show_frame(StartPage))
        button4.pack()

the main loop thing

app = SchoolProjict()
app.mainloop()

'''' i am sorry if it dose not make any sense

  • You can put a string into an `Entry` widget by calling its `insert()` method. Here's a little [documentation](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/entry.html). – martineau May 26 '19 at 18:33
  • @martineau when i used text1.insert(fname) on my function it say text1 not defined – Alaa Alharbi May 26 '19 at 19:58
  • i think i am doing it the wrong way – Alaa Alharbi May 26 '19 at 19:59
  • Actually the documentation link I gave you before was for a `tk.Entry`, not a `ttk.Entry` (which is [here](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-Entry.html)). However I _don't_ think the differences between the two is causing the issue—it's because `text1` is a local variable in the `SetingPage.__init__()` initialization method, which means it can only be referenced from within it. See the @Bryan Oakley's answer to the question [Calling functions from a Tkinter Frame to an other](https://stackoverflow.com/questions/48731097/calling-functions-from-a-tkinter-frame-to-an-other). – martineau May 26 '19 at 20:19
  • @martineau thank you very much i'll look into it now – Alaa Alharbi May 26 '19 at 20:28
  • Alaa Alharbi: You are welcome. Try to fix the problem(s) yourself, and if you still can't get it work for some reason, ask another question here. – martineau May 26 '19 at 20:33
  • [i posted this question again with some modified code and got the answer for it in the link ](https://stackoverflow.com/questions/56386715/how-to-entry-insert-work-with-frames-in-tkinter) – Alaa Alharbi Jun 01 '19 at 00:05

1 Answers1

0
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()))
        ...