Editing Russ Python Tips and Techniques
Jump to navigation
Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
= What = | = What = | ||
These are Tips and Techniques I have found useful in my Python programming [[User:Russ_hensel]]. I would be interested in your feedback. Let me know if you think the tip is useful/wrong/something everyone knows. A good set of this material relates to applications with a GUI. They are often not beginner techniques, but neither are they all advanced ones. | These are Tips and Techniques I have found useful in my Python programming [[User:Russ_hensel]]. I would be interested in your feedback. Let me know if you think the tip is useful/wrong/something everyone knows. A good set of this material relates to applications with a GUI. They are often not beginner techniques, but neither are they all advanced ones. | ||
− | Just starting Feb 2017 check history | + | Just starting Feb 2017 check history to see if progress is being made. If you find a tip not finished or working feel free to contact me. |
== Ones Still To Be Written == | == Ones Still To Be Written == | ||
Line 7: | Line 7: | ||
* when division by 0 is useful | * when division by 0 is useful | ||
* code is multiple files - scratch file | * code is multiple files - scratch file | ||
+ | * msg, print( msg ) | ||
* moving to 3.6 use fstrings | * moving to 3.6 use fstrings | ||
* example files with functions [[Python Example Code]] | * example files with functions [[Python Example Code]] | ||
− | + | [[Programming Tips]] | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
= Parameters for Applications = | = Parameters for Applications = | ||
Line 160: | Line 69: | ||
</pre> | </pre> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
= Python Code Example Files = | = Python Code Example Files = | ||
When I learn a new thing in Python I often keep an example file. I can then use it for reference and/or copy code out of it for other uses. I have a few "tricks". See: '''[[Python Code Example Files]]''' | When I learn a new thing in Python I often keep an example file. I can then use it for reference and/or copy code out of it for other uses. I have a few "tricks". See: '''[[Python Code Example Files]]''' | ||
− | + | ||
− | + | = Complex Tkinker Windows = | |
− | I found it really easy to get confused in coding Tkinter because it is not | + | I found it really easy to get confused in coding Tkinter because it is not a visual interface. |
− | One of my goal in coding is to make the code create the gui from top to bottom and from left to right. That way the layout of code and | + | One of my goal in coding is to make the code create the gui from top to bottom and from left to right. That way the layout of code and gui are easier to match up in my mind. This has a benefit of making it relatively easy to: |
* Cut and paste code to rearrange placement in the gui. | * Cut and paste code to rearrange placement in the gui. | ||
Line 253: | Line 88: | ||
* When using grids I always have variables for the row and column positions. These are typically managed with statements like lrow += 1. Coding this way make movement of the code much easier. Placement methods may also be used to manage the row and column variables. | * When using grids I always have variables for the row and column positions. These are typically managed with statements like lrow += 1. Coding this way make movement of the code much easier. Placement methods may also be used to manage the row and column variables. | ||
* Each individual frame is built in its own method, it takes the parent frame as an argument and returns the frame that it builds. The returned frame is then placed in its parent. | * Each individual frame is built in its own method, it takes the parent frame as an argument and returns the frame that it builds. The returned frame is then placed in its parent. | ||
− | * References to the widgets are often not needed once the GUI is built, so local variable are often used. Using a local variable makes it easier to copy/ | + | * References to the widgets are often not needed once the GUI is built, so local variable are often used. Using a local variable makes it easier to copy/paste. If I need a reference, I typically will do that on the last line of the code for the widget. For widgets that need references you can use an instance variable of the class, or in some cases it it useful to store them in a dictionary. |
* Building helper classes for either building the widgets, or placing them or both is often useful. | * Building helper classes for either building the widgets, or placing them or both is often useful. | ||
* You can see these techniques in GUI class of my various application documented on this wiki. | * You can see these techniques in GUI class of my various application documented on this wiki. | ||
* See: '''[[Python Buttons Switch Case]]''' for some tricks on building Tkinter frames with lots of buttons linked to functions. | * See: '''[[Python Buttons Switch Case]]''' for some tricks on building Tkinter frames with lots of buttons linked to functions. | ||
− | + | = Threading with Tkinker = | |
Tkinker wants to own the processing of the application. If you make a button start a long running operation then the GUI blocks and becomes unresponsive. So you need some sort of threading. Here are some tips. | Tkinker wants to own the processing of the application. If you make a button start a long running operation then the GUI blocks and becomes unresponsive. So you need some sort of threading. Here are some tips. | ||
− | == | + | == Polling withing Tkinker == |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
You can poll in the Tkinter event loop and call any function ( no arguments I think ) using a Tkinter function. Use this to run something short. You can only run it once this way. | You can poll in the Tkinter event loop and call any function ( no arguments I think ) using a Tkinter function. Use this to run something short. You can only run it once this way. | ||
<pre> | <pre> | ||
Line 322: | Line 129: | ||
</pre> | </pre> | ||
− | + | == Use Threading == | |
Use real threading. I inherit from threading.Thread and implement a override of the run method. Use threading mechanisms to "message" back and fourth to the gui, Tkinter, thread. | Use real threading. I inherit from threading.Thread and implement a override of the run method. Use threading mechanisms to "message" back and fourth to the gui, Tkinter, thread. | ||
Seems to work fine. | Seems to work fine. | ||
− | + | = Calling across Threads = | |
If you have objects in 2 different threads calling across the threads can be very dangerous. I have worked out a method that I find useful/safe/easy. It does make some assumptions. | If you have objects in 2 different threads calling across the threads can be very dangerous. I have worked out a method that I find useful/safe/easy. It does make some assumptions. | ||
Line 333: | Line 140: | ||
*Both objects/threads have something resembling polling where checks can be made at a convient time to see if the other thread wants attention. | *Both objects/threads have something resembling polling where checks can be made at a convient time to see if the other thread wants attention. | ||
− | + | == Step 1, Set Up Queues == | |
Line 372: | Line 179: | ||
more coming...... | more coming...... | ||
− | + | = Starting a Tkinter App = | |
These applications start fine but often remain in the background with their window behind other windows. It would be good to bring it to the top of your windows. This code, may not be the best, seems to work at least on the Windows. | These applications start fine but often remain in the background with their window behind other windows. It would be good to bring it to the top of your windows. This code, may not be the best, seems to work at least on the Windows. | ||
Line 381: | Line 188: | ||
You need to get this to run once after the mainloop is started. You can do this with '''root.after()''' Some details left to you. | You need to get this to run once after the mainloop is started. You can do this with '''root.after()''' Some details left to you. | ||
+ | |||
= Simple Techniques = | = Simple Techniques = |