Tkinter.Entry text variable

Post here if you need help with creating a Graphical User Interface in Python.

Tkinter.Entry text variable

Postby JessiT » Wed Jun 05, 2013 10:56 pm

Having trouble with my code
I want the input1 from Tkinter.Entry(text.variable=input1) to be the filename found using askopenfilename
here is my code

Code: Select all
import Tkinter, Tkconstants, tkFileDialog
import os

class Estimator(Tkinter.Frame):
 
    def __init__(self, parent):
        Tkinter.Frame.__init__(self, parent, background="white")

        self.parent = parent
        self.parent.title("Window 1")
        self.grid(sticky="w"+"e"+"n"+"s")
        self.LabelF()
        self.BoxF()
        self.SelectF()

    def LabelF(self):
        self.LabelF = Tkinter.Label(self, text="Label:", bg="white")
        self.LabelF.grid(sticky="w", column=1, row=1)

    global input1   
    input1 = "text"
       
    def BoxF(self):
        self.BoxF = Tkinter.Entry(self, textvariable=input1, fg= "red", border=4)
        self.BoxF.grid(sticky="news", column=2, row=1, padx=5, pady=5)

    def SelectF(self):
        self.SelectF = Tkinter.Button(self, text="Select...", command=self.askopenfilename)
        self.SelectF.grid(sticky="e", column=3, row=1, padx=5)

        self.file_opt = options = {}
        options['defaultextension'] = '.csv'
        options['filetypes'] = [('csv files', '.csv')]
        options['parent'] = root

    def askopenfilename(self):
        filename = tkFileDialog.askopenfilename(**self.file_opt)
        if filename:
            input1 = (os.path.normpath(filename))
            print input1
            v = open(filename, 'r').readlines()
            print v[0]
           
if __name__=='__main__':
    root = Tkinter.Tk()
    Estimator(root).grid()
    root.mainloop()
Last edited by JessiT on Wed Jun 05, 2013 11:00 pm, edited 1 time in total.
JessiT
 
Posts: 7
Joined: Wed Jun 05, 2013 10:53 pm

Re: Tkinter.Entry text variable

Postby metulburr » Wed Jun 05, 2013 10:58 pm

Please use code tags
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1471
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Tkinter.Entry text variable

Postby octopez » Thu Jun 06, 2013 8:57 am

Hi,

I don't know, if it has any impact on your code, but I don't cosider it to be good to name your methods of class Estimator the same as the widget objects. Nevertheless, I played around with this a little bit and this is the outcome: you need a Tkinter.StringVar and all is fine. See the "Effbot" for reference -> http://effbot.org/tkinterbook/

Code: Select all
import Tkinter, Tkconstants, tkFileDialog
import os

class Estimator(Tkinter.Frame):
 
    def __init__(self, parent):
        Tkinter.Frame.__init__(self, parent, background="white")

        self.parent = parent
        self.parent.title("Window 1")
        self.grid(sticky="w"+"e"+"n"+"s")

        self.LabelF = Tkinter.Label(self, text="Label:", bg="white")
        self.LabelF.grid(sticky="w", column=1, row=1)

        self.entryContent = Tkinter.StringVar()
        self.BoxF = Tkinter.Entry(self, textvariable=self.entryContent, fg= "red", border=4)
        self.BoxF.grid(sticky="news", column=2, row=1, padx=5, pady=5)

        self.SelectF = Tkinter.Button(self, text="Open", command=self.askForFile)
        self.SelectF.grid(sticky="e", column=3, row=1, padx=5)

        self.file_opt = options = {}
        options['defaultextension'] = '.*'
        options['filetypes'] = [('All files', '.*')]
        options['parent'] = root

    def askForFile(self):
        filename = tkFileDialog.askopenfilename(**self.file_opt)
        if filename:
            self.entryContent.set(os.path.normpath(filename))
           
if __name__=='__main__':
    root = Tkinter.Tk()
    Estimator(root).grid()
    root.mainloop()


Sidenote, if any Tkinter-user reads this. I have nearly no experience with Tkinter, so there might be a more elegant way to solve the problem. Does anyone know, how to resize widgets automatically in Tkinter, once they're created? wxPython has it's sizers and they respond to changes of the content. I'm sure Tkinter is capable of this as well, but I don't know how. Would it be possible to change the width of the entry, after the StringVar has changed?? Thanks!
Greetings and all the best,
Octopez

Strange women lying in ponds distributing swords is no basis for a system of government.
User avatar
octopez
 
Posts: 25
Joined: Fri Feb 08, 2013 11:26 pm

Re: Tkinter.Entry text variable

Postby JessiT » Thu Jun 06, 2013 5:42 pm

Code: Select all
import Tkinter, Tkconstants, tkFileDialog
import os

class Estimator(Tkinter.Frame):
 
    def __init__(self, parent):
        Tkinter.Frame.__init__(self, parent, background="white")

        self.parent = parent
        self.parent.title("Window 1")
        self.parent.columnconfigure(0,weight=1)
        self.parent.rowconfigure(0,weight=1)
        self.columnconfigure(2,weight=1)
        self.grid(sticky="w"+"e"+"n"+"s")

        self.LabelF = Tkinter.Label(self, text="Label:", bg="white")
        self.LabelF.grid(sticky="w", column=1, row=1)

        self.entryContent = Tkinter.StringVar()
        self.BoxF = Tkinter.Entry(self, textvariable=self.entryContent, fg= "red", border=4)
        self.BoxF.grid(sticky="news", column=2, row=1, padx=5, pady=5)

        self.SelectF = Tkinter.Button(self, text="Open", command=self.askForFile)
        self.SelectF.grid(sticky="e", column=3, row=1, padx=5)

        self.file_opt = options = {}
        options['defaultextension'] = '.*'
        options['filetypes'] = [('All files', '.*')]
        options['parent'] = root

    def askForFile(self):
        filename = tkFileDialog.askopenfilename(**self.file_opt)
        if filename:
            self.entryContent.set(os.path.normpath(filename))
           
if __name__=='__main__':
    root = Tkinter.Tk()
    Estimator(root).grid()
    root.mainloop()


Thanks for the help. Columnconfigure and rowconfigure will allow your window to change size (see code above).
JessiT
 
Posts: 7
Joined: Wed Jun 05, 2013 10:53 pm

Re: Tkinter.Entry text variable

Postby micseydel » Fri Jun 07, 2013 6:04 am

Moved to the GUI forums. Please post all future Tkinter code in this section, and if you want help with non-GUI things, the code posted in another forum ought not include any GUI related code.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1369
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to GUI

Who is online

Users browsing this forum: QX11 and 2 guests