如何Entry.insert()在Tkinter中使用框架? [英] how to Entry.insert() work with frames in tkinter?

查看:579
本文介绍了如何Entry.insert()在Tkinter中使用框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个程序,以帮助我在学校的工作,并且在此方面取得了很大的进步,但是现在我很混乱,我需要对如何使文件路径显示在Entry小部件上进行罚款.我在这里尝试了很多东西,但我可能没有理解它背后的想法,请帮我解决它

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

推荐答案

您需要通过添加selftext设为类SetingPage的属性.

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

这篇关于如何Entry.insert()在Tkinter中使用框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆