An Introduction to Tkinter

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I guess nobody's here to do the time Council my name is Jeff Armstrong and today I'm going to talk to you a little bit about TK inter I don't know how many people are familiar with TK ander but we can go down a little trip to show you what you can actually do with this GUI toolkit so if anybody's familiar with TK enter or TK enter with a lowercase T depending on what version of Python we're going to be talking about today this is a thin gooey wrapper around something called tcl/tk or tickle TK and it's basically a cross path platform user interface toolkit one thing that's very notable about TK enter is that it is the only graphical user interface toolkit that is in the Python standard library by default so that's a big advantage when we think about it but another detail about this is it's pretty simple to use it's a very simple user interface toolkit so that's one of the advantages of using it um now I should point out before we get started a little bit about me I'm mostly a casual TK enter user I write some small applications when I need a graphical user interface in Python I'm going to opt for TK enter usually so I have some experience with some of the other GUI toolkits namely I've done some win32 programming if anybody's actually done that when straight C all right and then I I'm pretty familiar with like Java swing user interfaces and stuff like that so I'm kind of comfortable working through coding a user interface in the sense that I'm actually writing code to define how things are going to be laid out and stuff like that now I do want to point out though that I'm not like a primary developer of TK and ER I don't know exactly what's going on underneath that's just none of my business as a casual user or developers just using the toolkit so there's some interesting opinions about TK inner and I usually run into about three types of people here there is first person first type of person is people have never used it they might have heard of it but they have probably not and they probably never touched it in their lives there's a small group of people who have used it and actually enjoy using TK enter and then I'd say the vast majority of people who have heard of it also hate it and you can notice that I didn't put use it and hey because a lot of them didn't necessarily use it they just know that they hate it sure another and the fourth kind who like TCL but have never used TK so so why do we care about TK enter in the first place now I'm going to point it out again TK enter is in the Python standard library and that is a big advantage and it's cross-platform like I said so we can get it working on Mac OSX we can get it working on Windows we can get it working on Linux UNIX all the BSD stuff like that it's going to work great it's extremely lightweight so it's a very small toolkit that's actually somewhat limits its functionality in some cases but the fact that it's lightweight means we're not shipping it with you know 20 or 30 megabytes of libraries that might not be on somebody's Windows computer something like that and also another thing to point out about TK is it's good enough a lot of the time we don't need to actually download and install like PI site or something like that to get access to the QT libraries because we're just building like a simple dialogue that's just going to automate some background tasks and we really don't care about so that's when it's actually good to learn and use TK enter and in fact it can be great it's not that limited we can actually use it to build very complex user interfaces so let's talk a little bit about the past TK inter has a very limited widget set initially so we're talking extremely limited so you might be frustrated right off the bat with what you can actually do with this toolkit in the Python 2 series it had very unwieldy imports so we're talking it didn't look anything like your usual standard library imports the TK entered with a capital T was being drawn in if you wanted file dialogues who were in very odd import statements I mean they weren't even under the TK inner module very strange very upsetting and quite frankly the user interface was atrocious it looked ugly on just about every operating system if you use Linux you would notice that TK inner apps usually defaulted to looking like motif or less teef applications and when we're talking about the you know 2000's that looks kind of dated at this point on Windows there was some really ugly interfaces - pretty bad actually why don't we just switch here and take a look at the past here so I have a few TK enter examples that I've included I'll give you a link to where you can find them but for the why don't we start with just running let's see okay so I'm running on Windows 7 right now okay this windows 7 pro and I've got all the arrow effects turned on everything's nice and pretty and fancy and now let's look at this TK window I created it's basically a simple thing with a label a menu and two buttons now the first thing you might notice is a this is a small example you might not be able to see too well but this doesn't look very nice these are nice giant square buttons if we open the menu you're going to say well what the hell is that line doing there it's kind of it just doesn't look like a native application so we'll say that it is pretty ugly alright and that's the path so you can see I ran that in Python 2.7 so there's a caveat there but if you using like Python 2.6 or something like that you're going to see the same thing now in the present time Python 3 point X actually changed a lot of the TK inner interface so in its I was actually happy to see that they kept it I've always been in fear that it would be dropped from the standard library but somebody thinks it's fun apparently who maintains it and decided to keep it in there for everybody one of the big changes that came with Python 3 is actually it introduced pythonic namespaces we're not importing this capital T TK ender from the standard library which just it really didn't feel right and actually if we import other parts of TK enter he'll start to see how awful and ugly and unpaid on ik it was the big thing that came with Python 3 point X actually came with Python 2.7 too but that I discovered later was a major improvement to TK and that's actually something called theme TK that people in TCL world actually have had this around for a while and it existed as its own module that you could actually use with your Python programs but with the Python 3 series that is now built into the standard library ok what place at what theme TK does is actually makes our teak enter look reasonable or good on a lot of different programs or a lot of different platforms now it's a little platform specific actually when I tried it on like a Mac with modern Python it didn't change anything but if you do it in Windows we can just take a look and see how much better things look this is my little TK inner program and for the moment we'll just ignore what's actually going on in the code but you can just see at the top I'm importing TK enter with a capital T at the top all I'm going to do is add one line so I'm importing the theme TK engine after I import TK enter we'll save that we'll go back down and we'll try running this again we'll see if this looks a little bit nicer well that is a fun solution and that's because I didn't do this right all right so I reran this and you can see there's a little bit of a difference here all of a sudden I have Windows 7 style buttons they're actually rounded corners again okay things look a little bit better I can resize it just to show you a little bit more of the bounds around the buttons you can see the file menu still has that silly line that's actually an option you have to disable but we'll avoid that for now but you can see that it actually themed my window now so I'm actually running with the Windows 7 look and feel or arrow or Vista whatever you want to call it for now but but all I did was actually just import ttk after import Atika enter and that fixed a lot of the appearance problems that I was having okay so what's getting into a little bit of a background of how Ek works TK basically just runs in a nice program loop that's kind of transparent to the developer so you're just going to start them a TK domain loop and that's just going to run your application so we're not handling messages necessarily by ourselves or anything like that basically we're going to build a tree of widgets so widgets are all going to have parents and by widget I mean like a button or a label or text box etc etc we're just going to assign those to parents and that's we're going to build our interface it's just like we can use frames to organize widgets and stuff like that there's things called layout managers which are going to actually handle how we're going to place our widgets within our windows or in our frames or in their sub frames or etc etc and I'm going to go through some of these I don't want to just this isn't just the whole content of the presentation here and then widgets can have events bound to them so TK operates in a callback system so you just bind an event to a certain widget like a button is going to want to respond to a click so you're just going to tell it what to do when a click happens you're going to say oh call this function or method so it's a pretty simple user interface or pretty simple programming paradigm where things get tricky is we're going to have to talk about these layout managers now well I think there might be some tools to graphically layout like TK applications I personally don't use them so I'm going to actually show you how I would do it programmatically I'm actually going to code my user interfaces there are basically three geometry managers and TK okay the first one is absolute now the idea behind that is saying I want a button to appear at you know pixel coordinates 100 comma 100 in my window or something like that and that's nice and it works but the problem is if we're doing resizing that gets ugly and kind of terrible looking very quickly what we actually want is a window that when we resize we can get things to change shape accordingly so TK enter provides two other layout managers that are pretty useful we have the pack manager which is a very simplistic system and then a grid layout which is um it allows you to separate frames and windows into a grid system and assign widgets to certain cells in that grid a lot of people who might have worked with Java might be familiar with this if they've used like Java swing to actually design a user interface so let's look first at the pack pack layout this is extremely simple so basically we take a root frame or window all we're going to do is assign each widget aside we're going to say hey stick to the top of this window stick to the left side stick to the right side that kind of idea and the parent can size itself to just fit everything so if you put like four buttons in a window the window just makes itself the right size to fit the four buttons and the buttons will size themselves to make them fit the text that's in the button that kind of thing problem is this a dozen isn't very flexible so let me go back and just show you that one program we just ran all right now this is actually using the pack layout manager now if I go and open up the source code for this can everybody see that is that coming in all right okay but this class is actually going to define my user interface I'm just calling it without TT KX that's what it was originally here so basically I'm assigning a frame to my window right here self dot frames will frame route is going to be my root node of my TK application so that's the parent of everything that I'm going to be doing okay and I'm just going to call this function a build window so if we go down to build window I added three things if you recall there's a label and then two buttons terrible and not too shabby right so I add a label first I say how do I look that's the text I want my label to show and I'm actually telling it to put it on the top of the window so I say pack side equals top now with the buttons if I want to say it looks terrible what I'm doing is I'm selling the layout manager to put it on the left side of the window and then for the good button I want on the right side of the window in the product of all this of course is that window we saw originally now my problem with the pack layout manager is it's a little bit unclear as to what the top is and if these buttons should be on the side like I think by that definition that I just showed you with saying one buttons on the left one buttons on the right one buttons on the top I at first thought maybe the label would be between the buttons I don't know you know it's a little bit unclear and when I resize it it looks well a little bit silly right my buttons are following the sides of the window it's probably not the behavior I want in an application I'm just guessing maybe I want the buttons to stay kind of close together or all on the right something along those lines so the pack layout manager gets a little unwieldy to work with one thing that it does handle though as you've noticed is it can do resizing quite seamlessly so the parent window as it changes shape is telling it it's actually moving the widgets around as it needs to so that's all well and good but we can look instead at the grid layout manager and this is going to be a much better way to make a far more like complex user interface you can see that grid layout is a little bit more it's a little closer what you might see in some other GUI toolkits where you might actually lay things out in code gtk actually has a similar type of layout manager swing as I said earlier can do something like that so basically when you take a frame of your window so in your window you can actually tell it to layout in a grid and I can tell it to put a button in the bottom right or column and row or something like that and then resizing you can actually tell it what things should be resized you can say I don't want you to resize um actually that second bullet is actually I believe incorrect completely so please ignore it so we can look at that application we just ran and if we were to do something like the grid layout manager let's say we want it now this would be easy in the pack layout manager but let's just say for the moment the we want to put that label in between our buttons okay so we want to put that label so all we would have to do is in this pack layout manager that I have already will get rid of this where should we put the label should put on the left or in the middle maybe we have a preference we'll do it on so as you can see all I'm doing is just telling it what row and column I want my label in so I'm going to put it Rosi row column one for the bad button we'll just do that in again row equals zero column equals zero okay so all I've done is actually just changed around how I'm going to display this let's see if this works if anybody sees me make a mistake just yell so okay so now I've actually put my label in between my buttons kind of what I thought would happen when I did the pack layout manager at first but it wasn't so and if we resize this it's going to stay on the left because I didn't actually set any arguments that's going to assign how things are supposed to work we can do that with something called a sticky argument so if I go back here and I say let's say we want that good button to stay on the right side of the window all the time I can just add a sticky equals and we're going to use a directions like north south east west for this right so we'll say East is the right so I would just give it a tug look where I wanted to go if I wanted to sit in the bottom corner why don't we just do um south and east okay now when I resize it looks exactly the same because I'm using a pack layout manager for the parent so it's just sizing the window to fit everything right it's not doing anything beyond that if I didn't work very good well and South should have put it on the bottom you know what's happening here now I think about it um let's see here one thing we can try is we have arguments to allow sizing of self frame so what i'm doing here is i'm telling it when you resize configure we want this to be zero i want when i resize the waiting on row zero is highest so when you're expanding this row zero will be the first one to resize bigger so if I pull my window if I had multiple rows row zero is the one that's going to grow if row one it has a lower weight let's say so you can try things like I actually have an example that actually just does this so there we go so now in this window wrote zero is actually resizing because we've assigned it a weight now it knows something at or it something has to expand when this window expand I think that's what's happening so that's the wonders of the grid layout manager in TK and I hesitate to say of an expert at it a lot of the work I do it's a lot of finagling it into the way I want it to behave but it's not particularly ugly or drastically difficult to do a lot of this is probably because my window is using a pack layout manager to layout a frame that has a grid in it so sometimes I get a little confused on that is what is what's supposed to be happening well look at another example where that actually works so so widgets TK enter actually provides just calm intent simple widgets right this is kind of considered a shortcoming it just doesn't have things that are quote advanced widgets so things like combo boxes aren't n TK enter or or trees you just can't display those so that's one of its shortcomings it actually has just some very simple ones um we're talking buttons alright we all know to button is frames would just be a very simple organizational tool labels so this just text static text basically labels with images list boxes menus something called a scale which is actually if you can see in this picture over here this slider right here so you can just move it left to right and change that scale number you know from zero so you could read in a scale from that you can have spin boxes and obviously you can have like multi-line text widgets so I I actually have this up program in my source if you guys want to see at the end but it's pretty straightforward and simple but that just gives you an idea of all the widgets you can make with TK enter now I'm sure everybody says that's it you know this is very little here to work with right I've actually left another one off that is in TK enter and that's called canvas but I'm going to talk about that specifically at the end that's a little bit more customizable so ttk theme TK TT k in Python actually overrides the drawing of lots of these TK inter widget so it takes those buttons when you import it after TK enter it's actually going to override like button widgets and list box widgets and menu widgets and stuff like that and it's actually going to use the theme engine to draw them and that's why you get those things looking much prettier on windows there's probably a little bit more to it because you know it's using different libraries different common control libraries who it's not really important the other thing though that theme TK offers is a lot of modern widgets okay modern widgets things that you might expect out of a user interface um but one of the nice things is we can actually change the size style because this is theme TK right so we don't actually the example I showed you defaults to whatever operating system default it would be so on this one I believe it's going to be Vista native I think is the theme that you can use but um you have a lot of it see here if I were to execute those coat that little short a bit of code we can do see here import you'll notice that I'm in Python 3 at this point so all right we can do enter the ttk dot style dot theme names so i have a few available to me win native you'll see like claim all default classic on a lot of different operating systems that will be available on linux I believe Vista and XP native verb obvious are going to be the default views of different version of Windows so actually I don't know that Vista would so that gives you an idea of how the theme engine works and in theory you could probably write your own theme I don't know how you would do that though so let's walk through a few of these advanced widgets its theme TK finally brought to TK enter for us the first one would be like a notebook so when I'm talking about a notebook that's just a tab interface like you saw on the editor I was just word and right so you can just page through different TK inter frames using just tabs and it's got pretty sensible functionality of it's not I don't know if anybody's ever worked with like the win32 tab interface and how you have to program that it's extremely unpleasant it's not like you have pages it's like you just have buttons that happen to look like tabs so if we look at just a little quick sample of how that's going to work we can pull up notebook that's probably far too all right come on you all right in this simple program all I'm going to do is I'm going to create my frame that's going to be my base frame in my window okay and I'm a declaring I want to use a notebook right now I'm saying the parent I'm getting assigning it a parent so what I'm going to do now is I'm going to create two new frames and these are going to be pages within my notebook okay they're just frames you can see at first I'm going to assign them sizes and that's because we're just for this example they're going to be empty okay but I want to give them a default size so that my window or my layout manager doesn't just compress them down to zero because like I said it's always going to make things just fit um so what I do is I create my two frames I add them to my notebook right page one page two one of the nice things you'll notice here is that I don't have to actually deal with like the showing and hiding of the frames they're just going to the notebook handles that and that seems sensible and I'm glad that it actually works that way so and I'm going to put on the on this window to I'm going to add a label just says there's a notebook on the left and that really is only to show you how the grid layout engine works this example actually does work even on resize so you can see that I'm telling my grid here my parent frame that it's going to exist in row 0 column 0 of its parent which is the root TK node and then my notebook is going to be as a child of my frame is going to be in row 0 column 0 and then on the right of that it's going to be a label row 0 column 1 all right after that I just do some resize rules with that row and column configure stuff okay and that should be about it that should be all I need to make this now at the top you can see that we're going to start working in Python 3 from now on so you I'm importing TK enter with lowercase T and I'm importing theme TK as a sub module of TK enter that's how it exists nowadays and everybody should be using that in Python 3 let's not bother with the Python 2 interface okay so if I run this one here's my notebook example so you can see I just have these two tabs right here page 1 page 2 as I said they're going to be empty so not very interesting and then on the right side of the window there I have a notebook or just a label telling me that there's a notebook on this page and you can see that the grid system is now resizing properly so you can see that when it does resize that those row and column configure weights that I assigned at the bottom here you can see on the frames grid I said I want column 0 to be a weight of 2 during resize and I wanted the row of 0 with a weight of 2 so that means when it does resize it's going to prefer to resize that upper left quadrant of the grid over all the other quadrants specifically the labels column and that's why the resizing is working in this example so that's basically just a little introduction onto how to know how notebooks work this is considered an advanced widget and TK enter but I think most of you expect this out of a user interface toolkit at this point but when you're talking about using TK in ER this isn't quite the improvement the other thing that TK inter brought was a tree view yes another modern widget that only existed recently in TK but basically you could do this in TK with using like add-on toolkits that I'm not going to talk about and I never actually got into but with theme TK it's all built in fine now I would assume everybody in here is familiar with a tree view that's just you're here our kacal view of data I'm in teak in TK enter the tree view actually acts as like a column list - so you can use to organize like columns of data if you want as well so let's look at a quick example like I said you could have two different ways of viewing it like the here are couple data or you could just do it very simply all right so in this example I'm going to use a notebook again because I just want to show off using a tree and then using just a list of columns okay so I have two frames I'm going to and the notebook interface really isn't that interesting so let's just go down to where I actually call a self comm tree view and that's going to build my column tree view alright so this is pretty simple I'm going to assign the columns names and I actually like TK enters interface to this because it's using a much more in my mind it makes a lot more sense the columns are named I can access data in a column by name I'm not doing it by the data model underlying the tree view is actually quite simple as opposed to some of the other models like I used to have a lot of trouble wrapping my head around like the gtk system of doing things especially if you're programming and straight C and not using some sort of interface around it but the tree view I'm basically just assigning it columns I'm telling it it has a parent frame and I just told it right here on this row to do additionally show headings okay so that means I want to have my column headings available okay in this next loop I'm just assigning a width to each of my columns in this tree and I'm telling it that I want a heading with the text specified right so I'm using this the names of the data and I'm using the names of the columns to be the same okay and then all I'm going to do is insert some data into this these are the the insert statements here now one thing you might notice as the syntax used to insert data into the tree view the first argument is going to be the ID of the parent so since it's a tree view it can be in here in a hierarchical form but we don't actually want to do that here we just want to call them lists so the root note of our tree view is just a blank string that's the idea of it so it's very simple very straightforward I'm telling it I want at the end of my list and then I give it the values that I wanted to insert right so I'm giving it name and description example and then one row and the second value I'm giving it is example part two and another row then I'm going to quickly put in I'm using the pack window manager here and actually why I'm doing this is because I just want to see this tree view to fill up that whole notebook page okay so it has a parent frame I'm just saying fill the page I tell it fill both directions and expand equals one or true either one would work there okay so if I actually run this example oh do we still have a notebook available are yeah we can close that so we'll just do tree view and here you can see my column list right very simple and you can see when I select things it selects the whole row and you can do things with that data in the row you can detect when things are clicked and stuff like that I'm going to get it a little bit more into that shortly now the other tree view would be if I wanted to have like an expanding here are a list of data so let's move down to a tree tree view that I've assigned here okay this is very similar but you'll notice one thing when I do my first insert of data you can see that I'm actually retrieving the ID of that insert so I'm saying give me the node ID after I insert the data into the tree view and then when I do that I can insert new data at that as that parent node so that's why I pass ID to the next row saying please put a sub item on my first item alright now this is a very simple example so what I basically did is I differentiated between that column view in my tree view with here article data but you can actually combine them so I could have inserted columns and descriptions into this tree view too even though it has sub items and things like that so it's completely possible to do it that way too and again I just do a quick geometry call to fill the frame with that pack fill we both both again now I already have this program running so if I switch to my tree tree view you can just see what we actually have here and it's pretty simple I have an example example part two and I can expand these items and you can see I have sub items right pretty simple straightforward what's nice here is the data model that I'm that's underlying this is excessively simple I'm not building like some sort of class it's just a data model you could if you really want to get into some like MVC programming but sometimes you just want to get it done and pushed out so and you can see that it's selecting the whole row still so that would work well if I actually had descriptions attached to each of these sub items so I could select a whole row of data or something like that but that just gives you an idea of what the tree views look like one nice thing is actually you might have noticed this doesn't actually look at horrible in window still it's following the windows look and feel here very closely so that's one nice thing again about theme TK and how well it's doing here does that gives you an idea of the tree view theme TK offers a few other things that people would expect to have present anyway combo boxes check buttons radio buttons progress bars separators so that would be if you just wanted like a small horizontal line to separate portions of a window or something like that and then of course size grips so you can have that little corner that looks like you can grab it and pull it simple things like that but that's what theme TK brings to TK and I know this is maybe underwhelming if people haven't used a TK enter before you probably like so what this should be there anyway but here it is and so I'm just trying to point out that the widget set has expanded quite a bit it's not as so poorly implemented or not poorly in from the diet that's the wrong wording for it it's not so limited in its available widgets by default as it used to be now one thing I did want to show off a little bit was a canvas right the canvas and TK entry allows you to just do some drawing on the screen now this isn't actually theme TK so if you're really into Python 2-6 you can actually use this without adding any additional packages but what it does is it opens the door for like custom widget behavior so you can actually draw anything you want on a canvas you know just like you could on anything it offers primitive shapes so you can do things pretty simply so I have a little example of a canvas here let's open that up okay so this is a very simple program again all I'm going to do is create a canvas I'm going to sign it to my parents windows frame right and then doing some grit geometry management there and I'm just adding a rectangle onto my canvas so if we look at draw a rectangle it's pretty simple I'm just calling self canvas create rectangle and I'm giving it coordinates and then a width and height so pretty simple here and I've actually added a quit button to this just to give you some more examples of how grid Geometry manager might work so if we run this very simple application we'll get the extreme excitement of the square in a box there you go that's awesome right so that's basically how canvas works you can draw anything you want on it and I actually have a better example of that if we keep moving through here now in this example let me just stop here and point out that I'm using events properly with this button so when I ran that program right I can just click this quit button in an end right so why don't we just take a minute and we'll look at how that actually works on a button so when I declare my button that's going to be on this line right here I'm just assigning it to its parent I'm giving text equals quit and then the very last bit of this I'm sorry it's very difficult to see the screen up here with the lace I say command equals self duck quit right and now when we're talking about buttons what we're saying is whenever the button is clicked call this function or method right here so right in this example it's calling self duck quit and if we go down to self dot quit you can see it's just telling the frame to shut itself down so when the frame closes it closes the application because there's no window anymore okay so that's pretty simple and that's the ease at which we can actually assign events in TK enter so that's just a very simple idea of what the callbacks look like now if you have more complex interactions with components you can use a binding so that would be if you have an event that occurs on a component you can bind functions to be called when that event occurs now with a button totally unnecessary it's pretty simple but let's say with the canvas we want to do something with the canvas when somebody does something to the canvas so one very interesting thing we might be able to do is actually assign it some Mouse events we have for time so this example is the same as what I had before but there was control so you can see I'm adding my canvas just like we did before I'm drawing a rectangle on it I'm adding my quit button I'm handling some of the grid geometry right down here right and I'm packing now here's where things get interesting I'm calling self canvas bind and I'm assigning it event button 1 so what I'm saying in this call right here is when the left mouse button button 1 + TK enter is pushed call this function start line right when I move that mouse right so if my button 1 is down and there's motion of the mouse it's going to call draw line okay and then I'm assigning just an initial position 0 comma 0 so when I called start line you can see it receives an event and from that event right here we're going to pull an x and y out of that event and we're going to sign it to our initial coordinate now when the mouse moves we're going to do one other thing we're going to create a line from those initial coordinates to my new coordinates vente x - invent that Y and then I'm going to keep I'm going to store the new event coordinates so what this should do is as I move my mouse around the canvas it should draw little tiny lines between all the mouse points at a text in an event so when we run this see here canvas with mouse okay this looks exactly the same as it did right now when I bring my mouse up here I can draw some lines on here my release we're all done very simple and now one neat thing about this canvas I'm not really handling resize of the canvas at all here so I can resize the window and it looks fine right so I'm not like shrinking the contents of my canvas but one nice thing is that the canvas is remembering what was drawn after resized so I'm not actually losing that information so that's one way you can use the canvas now I've actually built like a much more not much more a more complicated application that might actually use canvas to some interesting degree I actually had a need to draw almost like a 2d like surface map not really surface map I don't know how to describe it exactly and I wanted to use Python 3 right but the matplotlib people haven't caught up with us so I had to not use matplotlib and I needed some custom way to just draw some colored squares so that's when canvas came to the rescue so if I do this so what this application is that I want to just show you really quick is something I wrote when I was doing some work for as a NASA contractor and what it's doing is it's a way to examine a database of aircraft engine tuner information I actually gave a presentation on this in by PyCon 2010 I think but anyway it's going to look at these tuners that determine the performance of an aircraft engine okay it determines how old or well performing our engine is operating at so this is a rather more advanced use of TK so all I'm doing here is I'm calling my application I'm telling it where it can find this engine information and it's just an SQLite database that it's going to be reading in but this whole thing is written in TK TK enter with theme TK and it gives you a little idea of how complicated a user interface could be and this is actually not even that complicated it's pretty simple I think it's tuners here we go all right so this is a pretty it's rather ugly but it gets the job done so what the idea here is we have these tuners that are available in three dimensions so the altitude Mach number and the speed of the engine spinning right and what I want it to be able to do is actually go into the database and examine what the average numbers are the performance numbers associated with each of those operational levels so when I go to tuners this is this fantastic plot routine I wrote but all it's doing is drawing directly on a TK canvas so this is very all custom and it actually resizes pretty well you can see I've actually abused some of the TK inter or theme TK widgets that are new I have a combo box and everything on here we can change it and look at different altitudes and the NFR would be a fan speed how fast our engines spinning and that kind of thing the other thing we needed to be able to do was like delete bad data from this database so I can look at how old the data is like when it was last updated in the database and things like that so it gives you some sort of measure of how old things are how old the performance data is and I can actually go through and remove it from our database using this combo box or this is actually a tree view I'm sorry and you can see I'm just using columns it's not here our kacal so it just looks like columns in this example but this gives you a little bit of an idea of what you can actually do this is all just TK enter it's not particularly difficult to program something like this yeah basically yeah so that gives you some idea of what you can actually do TK ener offers a few other things just want to mention we have standard dialogues and you get a file dialog that would be as TK entered file dialog or TK file dialog that's the fun of using different versions of Python color choosers message boxes there might be a font selection box I'm not particularly sure but you can see that I have two different imports that you would have to use here and that's actually because there's some differences between Python 2.7 and three point X so as I said python 3 is a lot more pythonic and how you would import this that's why you would import if we go back TK enter dot file dialog right the file dialog should be in the TK inter module in Python 2 point whatever it was not you'd have to import TK file dialogue it's very messy but actually one thing I found is it's not too difficult to do cross version applications in TK enter because we know what the import statements change to but the actual code underneath like the syntax for accessing TK enter really hasn't changed so all we really need to do to fix things that are going to change versions if we want to run on the Python to seven or three point whatever we just need some import error magic at the top of our program so we can just try to import TK enter with a capital T if we get an import error than we import TK enter with a lowercase T and everything should work if we're going to use those standard dialogues though you might have to do some import as kind of calls and stuff like that but it's actually possible and you can if you feel the need to support Python 2.7 it's not that hard so for documentation I find the Python standard library documentation of TK enter to be miserable personally it's just there's with the standard TK widgets there's almost nothing there I think if you go into the actual module and documentation it's just going to say a crash course in TK and it doesn't really provide much the theme TK data error documentation is quite a little bit better but I would suggest everybody if you want to try to do a TK inter application TK dot-com is a relatively new site that I actually came across it has some references for creating any of these widgets using TCL Ruby okay mo and of course Python in it the nice thing is they're displayed all in a row so you get an idea of what they'd look like in different languages so anyway I hope some people took away from this that TK and ER can be pretty sweet right it ships with almost every version of Python that is just a big wind so we don't have to try to import the libraries that wrap QT or anything like that we can just use TK enter it's right there if you have something simple to write it now has modern widgets with theme TK and it's not nearly as ugly as it used to be it's easy to build a quick GUI if you're used to doing that kind of programmatic layout so if anybody wants to anything else about this you can look at my github account I have some I have all this source code posted these are really simple applications so I don't know how much value they'd be to people presentation is also available and if you have any questions that's my twitter handle and email so at this point if anybody has any questions right now I'll be happy to answer them in tickle there is a there's an extension critical TK group also gives you advanced widgets like that but the main advantage is that gives you a way of composing widgets so that you can put them together ahead and things and package it as a widget that satisfies the same kind of widget interface and you do that kind of thing I don't know if there is an inky and tickle for Python I believe there isn't and one problem with those is I'm not sure how well supported they are still like I think they're falling behind rather quickly like I think it might even be very difficult to build on a certain systems so he was asking about a package called ink wrote ek and that did it allowed you to build widgets basically compose widgets that would behave like a tickle or TK widget as you would expect um I do not believe that's available in Python though but I could be mistaken yeah I'm not exactly sure I'm not sure you could build a widget from scratch using Python and teeka entry I understand what you're saying I think would be difficult but I don't know the specifics of it so I can't really talk to that sorry I believe there is and that would be a bind event I think so you would want to look up something I would guess it's capital D drag a capital A and capital D draw or something like that something along those lines and it'll be in those braces those a greater than less embraces I don't know what widget support it and stuff like that but I think it's there I personally but I haven't but I am not in the business of building Python graphical user interfaces I would say that once you get down to like let's say you've an application with tons of dialogues like you need to lay out text boxes stuff like that it probably would be easier to use something where you could actually do it graphically because you just need to get it done I mean programming these layouts might be a little rough by hand did you want to say something about that oh okay so it could be um if you're if you're generating a lot of Windows that might be a little bit of a mess honestly so II I believe there's one you can import I'm not sure I think I heard oh I'm sorry they he asked if there's a widget for TK enter that would give you an HTML view and I think there is one that wraps maybe a WebKit pane or something like that I I'm not sure and then when you start talking about like wrapping the WebKit pane now we're there's probably a lot of platform dependence that might be you know you know I'm not so while it's probably easy to get on Linux or Mac it's probably a pain in the ass to get running on on Windows just guessing though but I think there is one I'm not sure things like actually matplotlib can render to Tk onto a TK canvas so if you're working with like graphs or scientific data TK inter can be very nice but again if I don't know if matplotlib hit Python 3 yet so courage but rather than taking taking a comma-separated value table up for something like that he wanted to know if you could show like an Excel or spreadsheet data I would probably just opt to use that tree view with columns because you can programmatically change the number of columns let's say so if it's an arbitrary CSV that's really easy whether you would get the lines like you know actually grid it out would be I I'm not sure and you could you would probably be able to go to like fake row numbers I don't think you have like a heading a row heading kind of handle to anything but you could probably fake row numbers by saying the first column is the row or something like that yeah it's not ideal but it's it's definitely a solution so any other questions well thank you very much appreciate it
Info
Channel: Next Day Video
Views: 117,092
Rating: undefined out of 5
Keywords: pyohio, pyohio_2012, JeffArmstrong
Id: 6isuF_bBiXs
Channel Id: undefined
Length: 49min 4sec (2944 seconds)
Published: Sun Sep 02 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.