Hi
i'm totally new to python and I'm trying to make an easy GUI with tkinter that should take a name of a textfile in an entry widget, and when you click a button the text in the entry should be passed to a function handling the file.
This is a snippet of my code:

win = Tk()
win.title("Upprepade ord")

textFileButton = Button(win, text = "Läs in")
textFileButton.grid(row = 2, column = 2)

textFile = StringVar()
textFileEntry = Entry(win, textvariable = textFile)
textFileEntry.grid(row = 1, column = 2)

textFileLabel = Label(win, text = "Textfil")
textFileLabel.grid(row = 0, column = 2)

textFileButton.configure(command = mainObject.file2List(textFile.get()))

win.mainloop()

The problem is that the file2List function checks if the file passed exists, and produce an error if it doesn't.
This error now shows as soon as I run the program, not after passing a faulty filename.
So, how do prevent the function being run until i click the button?

Recommended Answers

All 5 Replies

Something like this:

# -*- coding: cp1252 -*-
from Tkinter import *
from ScrolledText import *

def filein():
    ##    mainObject.file2List(textFile.get())
    mywin = ScrolledText()
    mywin.pack(fill=BOTH, expand=YES)
    mywin.insert(END,open(textFile.get()).read())
    

win = Tk()
win.title("Upprepade ord")

textFileButton = Button(win, text = "Läs in")
textFileButton.grid(row = 2, column = 2)

textFile = StringVar()
textFileEntry = Entry(win, textvariable = textFile)
textFileEntry.grid(row = 1, column = 2)

textFileLabel = Label(win, text = "Textfil")
textFileLabel.grid(row = 0, column = 2)

textFileButton.configure(command = filein)

win.mainloop()

Lycka till!

Thanks
I'm having problems importing ScrolledText:

File "pUppgift.py", line 12, in <module>
    from ScrolledText import *
ImportError: No module named ScrolledText

Is it possible to solve without scrolledtext?

>>> import ScrolledText
>>> ScrolledText
<module 'ScrolledText' from 'D:\Python26\lib\lib-tk\ScrolledText.pyc'>
>>> import sys
>>> sys.version_info
(2, 6, 4, 'final', 0)
>>> [/B]

I left your function in comment but had to test the functionality with one standard library component. Take out my stuff and uncomment your call.

Ok, thanks for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.