Showing posts with label tkinter. Show all posts
Showing posts with label tkinter. Show all posts

Monday, 18 May 2015

Python 3 - Open File Dialog Window In Tkinter With filedialog

I wanted to make a file loader for a chatbot program I am working on in Python 3 using Tkinter. I thought  that it would be a tricky task until I stumbled up the filedialoge module and the askopenfilename function.

Tkinter uses the filedialoge access the askopenfilename to open a window that matches the format of your operating system. Mine is currently Windows 8, so it looks like this.


How good does that look. To use the askopenfilename function you need to import it:
from tkinter.filedialog import askopenfilename
Because you will probably want to run the file from a command in a menu bar of from a button click it's probably a good idea to wrap it in a function:
def OpenFile():
    name = askopenfilename(initialdir="C:/Users/Batman/tkinter/",
                           filetypes =(("Text File", "*.txt"),("All Files","*.*")),
                           title = "Choose a file."
                           )
The askopenfilename function can have a number of options or no options and you can leave it blank. The ones I have used in this example I think are the most useful:

initialdir 

 set the the initial file location that the function will open it.

filetypes


This allows you set the type of files you want the user open. The end result appears in the bottom drop down menu.
First you set the file in a tuple ("Text File", "*.txt"). The first input is the title you want to name the file type(e.g. "Text File") and the second string is the file name ("*.txt"). If you have a custom file type, like ("*.noob"), then you can use that too.
What I discovered is that it requires a minimum of two file types for this option to work. So if you get an error this might be it.

title

This is the title you want to add to the top bar. 


In the below example you can execute the file in the Menu in File-Open. I thought I'd give you a realistic application of the askopenfilename.
As you can see from the first print the file does not open the document, rather it gets the file location and from there you can open and use the file. In the above example I have opened the file as a variable and then read it because I had a text file. You could as easily put the variable in a label or text widget in tkinter.

Useful Links:
http://effbot.org/tkinterbook/tkinter-file-dialogs.htm
http://tkinter.unpythonic.net/wiki/tkFileDialog

Saturday, 16 May 2015

Changing The Colour Of Selected Text In Tkinter Text Wiget In Python

I am working on a simple chatbot program using Tkinter in Python 3 and have finished all the back end.
I'm now working on making the front end look pretty.

The Problem
One of the things I wanted to do was change the colour of the chatbot's title in the text widget to make it easier to read. I thought it would be pretty easy.
My guess was that I could modify the colour from the .insert method. A little like this:

text.insert(END,'Bottity Bottity Bot Bot: '+ BotInput + '\n',foreground='red')
Fail!!!
Life isn't that easy.

The Solution
It turns out that it is quite a task to add a colour to words in a text widget in Tkinter. The best hack I could find was to modify a search and highlight function I found in Python in a Nutshell by Alex Martelli.


I had to make some changes which basically removed the search function and replaced it with a key word of the name of my chatbot, in this case: Bottity Bottity Bot Bot

Here are the results in a simple example:

As you can see, the find() function searches for my chatbot name and then changes it's foreground color in the text.tag_config('found', foreground='red') section. in tag_config you can also modify the font styles and the background.

P.S. I really like the lastidx = '%s+%dc' % (idx, len(s)) variable. This was such a clever bit of code that I spent an hour down the rabbit hole on String Formatting Operations.

Friday, 15 May 2015

Python 3 - Tkinter Scrolling with Inserted Text

I am using tkinter's ScrolledText to create a text box that I insert text in each line.

The problem
The problem was that when I inserted new text, the text box would not automatically scroll down to reveal the new text.

The solution
The solution turned out to be the yview method.

In the example below, I added while loop of 100 numbers(lines) to demonstrate that noew the text box is now focussed to the bottom of the text.