Organize tkinter application - python

I have a question about application programming using Python and tkinter.
All the simple examples I see in tutorials use one class for all the widgets and all the bound methods. I decided to do this from the start since I saw no other examples, not thinking anything about it. As my application has grown, I've got a lot of methods in this one class, and it's getting kind of ridiculous.
Luckily, I am designing a front end for an application that I had already made for the console, so the application logic itself is contained in another class, but I still have a lot of methods in my one front end class.
Is there some other way to do this I'm missing?

Maybe you can follow the MVC design pattern (Model-View-Controller):
You keep your application logic in its class (Model).
You separate your view to two parts: a Controller which contains event listeners and the View which contains the widgets.
I have done it this way for a Java application with Swing. From my modest experience with Python & Tkinter you can follow the MVC patters here to.
This link can inspire you MVC example with Tkinter

Related

Tkinter with or without OOP

Studying Tkinter and I've only found tutorials on Tkinter without OOP, but looking at the Python.org documentation it looks like it's all in OOP. What's the benefit of using classes? It seems like more work and the syntax looks night and day from what I've learned so far.
This is going to be a really generic answer and most of the answers to this will be opinionated anyways. Speaking of which,the answer will likely be downvoted and closed because of this.
Anyways... Let's say you have a big GUI with a bunch of complicated logic sure you could write one huge file with hundreds, if not thousands of lines, and proxy a bunch of stuff through different functions and make it work. But, the logic is messy.
What if you could compartmentalize different sections of the GUI and all the logic surrounding them. Then, takes those components and aggregate them into the sum which makes the GUI?
This is exactly what you can use classes for in Tkinter. More generally, this is essentially what you use classes for - abstracting things into (reusable - instances) objects which provide a useful utility.
Example:
An app I built ages ago with Tkinter when I first learned it was a file moving program. The file moving program let you select the source / destination directory, had logging capabilities, search functions, monitoring processes for when downloads complete, and regex renaming options, unzipping archives, etcetera. Basically, everything I could think of for moving files.
So, what I did was I split the app up like this (at a high level)
1) Have a main which is the aggregate of the components forming the main GUI
Aggregates were essentially a sidebar, buttons / labels for selection various options split into their own sections as needed, and a scrolled text area for operation logging + search.
So, the main components were split like this:
2) A sidebar which had the following components
Section which contained the options for monitoring processes
Section which contained options for custom regular expressions or premade ones for renaming files
Section for various flag such as unpacking
3) A logging / text area section with search functionality build in + the options to dump (save) log files or view them.
That's a high level description of the "big" components which were comprised from the smaller components which were their own classes. So, by using classes I was able to wrap the complicated logic up into small pieces that were self contained.
Granted, you can do the same thing with functions, but you have "pieces" of a GUI which you can consider objects (classes) which fit together. So, it just makes for cleaner code / logic.
Like what pythonista just said...
OOP makes your GUI code more organized and if you need to create new windows eg.toplevel() you will find it extremely useful because you won't need to write all that code again and again and again... Plus if you have to use variables that are inside another function you will not need to declare it as a global. OOP with Tkinter is the best approach

Only one Class possible for GUI programming

I'm new to GUI-programming and using now tkinter for python.
In the past my "non-GUI" programs always consisted out of a few classes but if I look to the examples with a GUI it appears that only one class is used. All functions are included in this one class. Is this the normal way or is it possible to write a gui class which "calls" functions from other classes?
As I look at it now it seems the concept of object oriented programming dissapears by implementing the GUI in an OOP manner
It is definitely possible to use multiple classes in GUI apps.
For example you can have one class which defines and layouts GUI elements (like buttons, text fields, scrollbars etc.) and the second class would subclass it adding some functionality on top of it.

How to receive input from GUI, apply function, then display?

I wrote a function that receives input as a string, modifies it, and outputs a string. I want to make it so that my non-computer savvy friends can use it, by adding a graphical user interface. I want something very simple: A box where they type in the text, a button that along with hitting "Enter," submits the text, and then a place where it displays the result after my function had modified it. I just want a way to receive input and to write output to a GUI in a way that a regular person can understand. I have no experience with GUIs.
Update
In the end I used XCode to create the GUI and PyObjC to pass the data from GUI to Python in a sort of "frontend-backend" setup.
You may be interested to get a look at http://zetcode.com/
there is a bunch of tutorial about wxPython, PyGTK and PyQt
It should guide you.
Jordi
Does your program have to be python? (as tagged) Your description sounds like a very straightforward task. In that case, have you considered JavaScript + HTML? Your users wouldn't need to download and install anything new, and most people have a good grasp of how to create/use web page forms.
If you're committed to a python implementation: There are a variety of different GUI toolkits (ways of creating the graphical user interface), but which one you choose will depend on how you want your program to look, what operating system you run on, ease of programming, and a variety of factors.
The widgets you describe are fairly simple (text box and button), and you might be able to accomplish your goals using Tk / Tkinter, python's "de facto standard GUI". The advantage of this GUI toolkit is that it's bundled with python on most operating systems, hence (so long as your friends have python installed) they're ready to go. If you don't have experience building complicated installers, then you'll find that users wander off when you require them to install a dozen modules on their own. The TK script would also run on any OS.
Convenience aside, the disadvantage of Tkinter is that Tk is a fairly old and limited framework in the form commonly found with python, and it can be painful to work with for more complicated programs. (some of the online tutorials have typos like mixed case or missing quotation marks) For your task, though, the basic layout and code are pretty simple: see the TkDocs site for a demo that you can adapt.
http://www.tkdocs.com/tutorial/firstexample.html
If you are familier with C# , than you can use IronPython,
just import the Form libraries, make simple form, by putting Lable, Textbox and Button, and refer to that textbox value on Button click. you can get the WOW Gui for Python, with IronPython
Ref : IronPython

Python Web/UI Options

We are in the process of standing up a UI on top of a python system. This is all throw away code, so we want something quick, yet presentable.
We will have a couple of "interfaces" but they will be of two types. One will be control, it will basically be sitting on top of a python thread, and accepting requests from the user.
The other will be more of a display screen that will need to be able to display images, and some classic "grid views" of text to the user.
We pretty much know we could* do all of this in HTML but wasn't sure what would be the best way to interact with the core python code?
Anyone know of a good UI python presentation layer? Since we know we can do all of this in HTML/Jquery pretty quickly, we are also open to suggestions on how to integrate this with a web server..
Any suggestions? Really interested in finding out if there is any way to use python as the back end to a webserver.
Let me know if you all need more information.
I like wxPython. The demo application is excellent and lets you browse, tweak and re-run the code right in the demo.
We have found the DJango meets our needs. It is a pretty slick mvc style python web stack. Really is easy to use, and very quick to develop in. I will say that the ORM layer is a little young so it is hard to do some simple queries, but luckly since this is throw away code we can just use native sql.
Tkinter is probably going to be the solution you can use quickest. Its API is simple and straight-forward, and you probably already have it installed.
As the other 2 classic Python GUI options have been given already, I feel duty bound to suggest PyQt :)
Using QT Designer I've found it much simpler than TKInter to get some basic GUIs up and running. Build your GUI up in a WYSIWYG way, then hook it up to the back-end logic. I've also found that the large amount of C++ help on QT available on the interent usually translates more or less directly across to PyQt. The resources available for TKInter are IMO pretty obtuse, and simply stop as soon as you want to do anything more interesting than Hello World. YMMV.
The Rapid GUI Programming with Python and QT book is a fantastic resource. Had me programming real applications in no time.

Need help/guidance about creating a desktop application with gui

I'm planning to do an Desktop application using Python, to learn some Desktop concepts. I'm going to use GTK or Qt, I still haven't decided which one.
Fact is: I would like to create an application with the possibility to be called from command line, AND using a GUI. So it would be useful for cmd fans, and GUI users as well.
It would be interesting to create a web interface too in the future, so it could be run in a server somewhere using an html interface created with a template language.
I'm thinking about two approaches:
- Creating a "model" (core module, where all the functionality resides) with a simple interface which is called from a desktop/web implementation;
- Creating a "model" with an html interface, and embeb a browser component so I could reuse all the code in both desktop/web scenarios.
My question is: which exactly concepts are involved in this project? What advantages/disadvantages each approach has? Are they possible?
By naming "interface", I'm planning to just do some interfaces.py files with def calls. Is this a bad approach?
I would like to know some book recommendations, or resources to both options - or source code from projects which share the same GUI/cmd/web goals I'm after.
Thanks in advance!
What about a link to the source?
Having a CLI command and a GUI front-end is really common on unix platforms... The TransmissionBT bit torrent client is a good example of what you plan to do.
Decoupling the presentation layer from application logic makes it possible!.
You can divide your application into layers, a layer is a reusable portion of code that performs a specific function.
For example, you can divide the application into two or more layers, one layer with the presentation of the application and the other with the model. This allows reuse of the entire model of the application with different implementations of the presentation layer.
The presentation layer contains the components that implement and display the user interface and manage user interaction. This layer includes controls for user input and display, in addition to components that organize user interaction. Can have multiple implementations of the presentation layer technologies such as PyQt4, PyGtk, Html, console, etc.
Why Separating Model Is Useful?
You may wonder why it is important to move as much logic outside the presentation layer and into the model layer. The biggest reason is reuse: logic placed in a model increases the reusability of an application. As applications grow, applications often grow into other realms. Applications may start out as a web application, but some of the functionality may later be moved to a smart client application. Portions of an application may be split between a web site and a web or windows service that runs on a server. In addition, keeping logic helps aid in developing a good design (sometimes code can get sloppier in the UI).
However, there are some caveats to this: it takes a little longer to develop applications when most of the logic resides in the business layer. The reason is this often involves creating several sets of objects (data layer and access code, plus business objects) rather than embedding it in the application. The extra time that it takes to do this can be a turnoff for some managers and project leads, especially because it often requires you to be knowledgeable about object-oriented programming, more than most people are comfortable with.
You can search about N-Layered architectural style on the Internet.
This is a sample application that implements several types of user interfaces, but based on. NET framework, for python is similar.
http://microsoftnlayerapp.codeplex.com/
I am working on a big project based on PyQt4 presentation framework. one of the styles of architecture applied is N-Layers. If you want a simple example based on PyQt4, send me a email.

Categories

Resources