Re: [Tutor] how to change the command "string" on a tkinter Button?

2018-07-01 Thread Chris Roy-Smith

On 01/07/18 02:17, Alan Gauld via Tutor wrote:

On 30/06/18 03:55, Chris Roy-Smith wrote:


I am trying to change the command of a tkinter Button in my program.
Eventually I want to be able to do this to many buttons.

Since I'm not 100% sure if you mean the command or the label or both
here is a simple example that does both...


import tkinter as tk

def cmd1(): print('This is command 1')

def cmd2(): print('This is number 2')

def swapCmd():
 c = b1['command']
 # kluge to get the function name from Tcl id
 if str(c).endswith("cmd1"):
 b1['command'] = cmd2
 else:
 b1['command'] = cmd1

def swapText():
 t = b1['text']
 if t == "Cool":
 b1['text'] = "Hot"
 else:
 b1['text'] = "Cool"

# make GUI
top = tk.Tk()
win = tk.Frame(top)
win.pack()
b1 = tk.Button(win,text="Cool", command=cmd1)
b1.pack()
b2 = tk.Button(win, text="Swap text", command=swapText)
b2.pack()
b3 = tk.Button(win, text="Swap cmd", command=swapCmd)
b3.pack()

top.mainloop()
###


Thank you Alan, you have covered what I think I wanted to achieve. For 
me, programming is a continual learning experience, unfortunately I seem 
to forget nearly as much as I learn, Python is the first language I have 
attempted since macro assembler for CP/M. Python seems to be another world.


It appears that I broke the code I started experimenting with, to try  
changing the command, and that may have added to my confusion.


I'll play with your example to try an understand what is going on.

Regards, Chris Roy-Smith

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to change the command "string" on a tkinter Button?

2018-07-01 Thread Steven D'Aprano
On Sun, Jul 01, 2018 at 03:32:59PM +1000, Chris Roy-Smith wrote:

> Python is the first language I have 
> attempted since macro assembler for CP/M. Python seems to be another world.

Yes indeed, high-level languages like Python *are* a radically different 
programming experience than low-level languages like assembler. The 
fundamental execution and data models of the languages are *very* 
different:

- assembler lives in a universe of bytes and words; there are few
  abstractions and you are dealing (very nearly) with the lowest
  level of flipping bits in hardware, or at least of moving bytes.

- Python lives in a world of big, complex abstractions like dicts
  and Unicode text and even objects as complex as "web server",
  and the fundamental operations are multiple layers away from
  moving bytes.

It's not surprising that this may require some re-adjustment of your 
mental model of how to program.


> It appears that I broke the code I started experimenting with, to try  
> changing the command, and that may have added to my confusion.

"Save As..." before engaging in big changes is your friend :-)

Even better would be to learn a form of VCS (version control system) 
such as Mercurial (hg) or git. Depending on the text editor you are 
using, it may have VCS integration available.

Off-topic:

I see you are a fellow Internode user, like me. Which part of Australia 
are you in, and is your internet connection giving you as much grief as 
mine is?

I'm pretty sure that Internode is *grossly* oversubscribed. E.g. when I 
try doing a google search, I'll usually get "Waiting for 
www.google.com..." which then times out about six or twelve times on 
average before succeeding to connect, after which it is damn near 
instantaneous.

I downloaded 2 GB of data in about ten minutes yesterday, not long 
followed by a 2K email that took *three hours* to leave my computer 
because the connection to Internode's mail server kept timing out.



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to change the command "string" on a tkinter Button?

2018-07-01 Thread Chris Roy-Smith

On 01/07/18 02:17, Alan Gauld via Tutor wrote:

On 30/06/18 03:55, Chris Roy-Smith wrote:


I am trying to change the command of a tkinter Button in my program.
Eventually I want to be able to do this to many buttons.

Since I'm not 100% sure if you mean the command or the label or both
here is a simple example that does both...


import tkinter as tk

def cmd1(): print('This is command 1')

def cmd2(): print('This is number 2')
I was hoping eventually to generate the command form the results of a 
database query, not knowing  the exact command until run time. Perhaps 
what I was trying to achieve is too close to self modifying code, I was 
warned off this when I used to dabble in assembler. Does the same advice 
hold for python?


def swapCmd():
 c = b1['command']
 # kluge to get the function name from Tcl id
 if str(c).endswith("cmd1"):
 b1['command'] = cmd2
 else:
 b1['command'] = cmd1
I never thought to try anything like this, I was thinking more along the 
lines for how to change the text of a Labe.l

def swapText():
 t = b1['text']
 if t == "Cool":
 b1['text'] = "Hot"
 else:
 b1['text'] = "Cool"

# make GUI
top = tk.Tk()
win = tk.Frame(top)
win.pack()
b1 = tk.Button(win,text="Cool", command=cmd1)
b1.pack()
b2 = tk.Button(win, text="Swap text", command=swapText)
b2.pack()
b3 = tk.Button(win, text="Swap cmd", command=swapCmd)
b3.pack()

top.mainloop()
###


Thank you again Alan,
While your solution works, it's not how I imagined in terms of approach. 
Eventually I wanted to change the command of a variable number of 
buttons with the actual command depending on the results of a database 
query. I am unable to see how I can manage this in your solution style ( 
predetermined commands) might be difficult to achieve ( with my limited 
programming skills ).  Your solution made me rethink what I was 
attempting to do, and found with a slightly modified database query, I 
didn't need to change the command at all. The rest of this bit of 
program I have already solved.

Now I just have to learn a lot more about classes, and objects.

again, thank you Alan.
Regards, Chris Roy-Smith
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] RAD GUI Development (like Visual Studio/VBA)

2018-07-01 Thread Shall, Sydney

On 30/06/2018 19:21, Alan Gauld via Tutor wrote:
> On 30/06/18 11:50, Shall, Sydney wrote:
>
>>> And if you want to try using Jython or MacPython(?)
>>> you can use the native GUI builders for those:
>>> - Eclipse/Netbeans (Java)
>>> - XDeveloper (MacOS) - I tried this once and it kind of works...
>>>
>> Alan,
>>
>> Could you expand a bit on the use of XDeveloper.
>> I have need of some such utility for my project and I use a MAC OS X.
> I've only gone through the tutorial and made some tweaks
> to the resulting code but it seemed to work pretty much
> as usual for Cocoa projects using Objective C.
>
> You layout the UI then connect the widgets to your code
> (which is in Python of course).
>
> The PyObjC project is here:
>
> https://pythonhosted.org/pyobjc/
>
> The tutorial I used is here:
>
> https://pythonhosted.org/pyobjc/tutorials/firstapp.html
>
> Note that it is old, but I only have an iBook from 2001
> running MacOS Lion, so age wasn't an issue for me!
> Thank you.
I shall spend some time studying the two sites and see what I can make 
of it.


Sydney



_

Professor Sydney Shall
Department of Haematology/Oncology
Phone: +(0)2078489200
E-Mail: sydney.shall
[Correspondents outside the College should add @kcl.ac.uk]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to change the command "string" on a tkinter Button?

2018-07-01 Thread Chris Roy-Smith

On 01/07/18 19:19, Steven D'Aprano wrote:

On Sun, Jul 01, 2018 at 03:32:59PM +1000, Chris Roy-Smith wrote:


Python is the first language I have
attempted since macro assembler for CP/M. Python seems to be another world.

Yes indeed, high-level languages like Python *are* a radically different
programming experience than low-level languages like assembler. The
fundamental execution and data models of the languages are *very*
different:

- assembler lives in a universe of bytes and words; there are few
   abstractions and you are dealing (very nearly) with the lowest
   level of flipping bits in hardware, or at least of moving bytes.

- Python lives in a world of big, complex abstractions like dicts
   and Unicode text and even objects as complex as "web server",
   and the fundamental operations are multiple layers away from
   moving bytes.

It's not surprising that this may require some re-adjustment of your
mental model of how to program.
those big complex bits cut down on the amount of code needed to achieve 
a given task. :-)




It appears that I broke the code I started experimenting with, to try
changing the command, and that may have added to my confusion.

"Save As..." before engaging in big changes is your friend :-)

yes, was supposed to be a quick experiment to test idea ;)


Even better would be to learn a form of VCS (version control system)
such as Mercurial (hg) or git. Depending on the text editor you are
using, it may have VCS integration available.
I don't know anything about these tools, I use Kate as my editor for my 
programming. I usually give a new number to separate versions, I'm sure 
there are better ways, I just haven't gone looking for them yet. Idle 
only looks useful for CLI stuff.


Off-topic:

I see you are a fellow Internode user, like me. Which part of Australia
are you in, and is your internet connection giving you as much grief as
mine is?
I have had little trouble with Internode over the last 13 years or so. 
The other day is the first time I had a really slow download, went to 
another mirror in Western Australia, and all went as fast as I'm 
supposed to get (I have the slowest option of NBN, ( bronze 100) which I 
have never NEEDED extra bandwidth, it might be nice, but I don't use the 
sort of service which depends on good bandwidth, downloads can go all 
night for all I care). I'm not greedy though it's all very fast compared 
to dial up, or packet radio


I'm pretty sure that Internode is *grossly* oversubscribed. E.g. when I
try doing a google search, I'll usually get "Waiting for
www.google.com..." which then times out about six or twelve times on
average before succeeding to connect, after which it is damn near
instantaneous.
I search with DuckDuckGo, and rarely have any response issue with my 
searches. I don't think they sell my search data.


I downloaded 2 GB of data in about ten minutes yesterday, not long
followed by a 2K email that took *three hours* to leave my computer
because the connection to Internode's mail server kept timing out.

Your connection is a bit faster than mine.
I've not experienced any speed issues with the email server.

Regards, Chris Roy-Smith
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to change the command "string" on a tkinter Button?

2018-07-01 Thread Alan Gauld via Tutor
On 01/07/18 09:15, Chris Roy-Smith wrote:

>> def cmd2(): print('This is number 2')
> I was hoping eventually to generate the command form the results of a 
> database query, not knowing  the exact command until run time. Perhaps 
> what I was trying to achieve is too close to self modifying code, I was 
> warned off this when I used to dabble in assembler. Does the same advice 
> hold for python?

writing sel;f modifying code is so difficult to get right and
so easy to be catastrophically wrong that its best avoided
in any language. Even guru level programmers get that stuff
wrong.

But it sounds like you probably want data driven code which
is (still tricky but) a lot easier. Tell us more with some
examples of the data and we can probably help.

> While your solution works, it's not how I imagined in terms of approach. 
> Eventually I wanted to change the command of a variable number of 
> buttons with the actual command depending on the results of a database 
> query. 

Sure, but the actual changing of the command will
still look like my code I suspect.

The tricky bit is creating the appropriate function
object so that you can assign it,.

The number of widgets is just a matter of a list
and an iterator

And remember, in a GUI everyting is event driven so you need to think
about what event will trigger this reassignment of commands.

You need the GUI in place and some kind of event catcher
(button?, timer?) to handle the changeover.

> attempting to do, and found with a slightly modified database query, I 
> didn't need to change the command at all. The rest of this bit of 
> program I have already solved.
> Now I just have to learn a lot more about classes, and objects.

OK, Glad you avoided the tricky stiff. Its very rarely needed.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to change the command "string" on a tkinter Button?

2018-07-01 Thread Alan Gauld via Tutor
On 01/07/18 11:33, Chris Roy-Smith wrote:

>> Even better would be to learn a form of VCS (version control system)

> I don't know anything about these tools, 

VCS store versions of a file. You can check for differences between
versions, restore previous versions, even merge different versions.
Modst also allow you to freeze a collection of file versions for a
release so that you can restore a previous version of your project.
They are what all professional programmers use on big projects.

One of the oldest and simplest VCS tools is RCS (Revision Control
System). It basically has 2 commands:
ci - checkin a file.
co - check out a version.

So you create a file and get it working.
Create a folder in your project folder called RCS
Check it in and keep a copy for editing

ci -l myfile.py


Now make changes until you have a new version
check it in
ci -l myfile.py

Realize that you made a mistake, get the original
version back

co -l -v1.1 myfile.py

and so on.
At each check in the system will ask you to
describe your changes

There are other commands you can use to query the
versions etc. The system only stores the differences
between versions so doesn't use a huge amount of storage.

https://www.gnu.org/software/rcs/

More recent systems work on servers across a network
and tend to operate at the project/folder level rather
than individual files, but the basic ideas remain
the same. Steven mentioned Mercurial and Git, there
is also Subversion(SVN) and CVS(based on RCS and
by the same team). There are also commercial tools,
some costing up to $1000 per seat! Its big business.
One of the best is ClearCase (now owned by IBM?),
nearly magical in its capabilities but very expensive!

Some editors will have menu commands to automatically
do the check in/out operations for you once you configure
the tool and paths in the properties.

> I use Kate as my editor 

Perfectly fine tool, no need to change unless you want
to.

> ...Idle only looks useful for CLI stuff.

Not at all it has a full file editor and integrated
interpreter so you can save your file and hit F5 to
run the code with the output in the "shell window"
(which is what appears at startup)

Even better is IDLEX which has a lot of extra features
such as tabbed editing windows, code folding,  line numbering,
intelligent paste from the shell, etc etc...

Many IDLEX features are planned for future IDLE releases,
but the approval process takes time. Meanwhile IDLEX
is available here:

http://idlex.sourceforge.net/features.html

And all written in Python/Tkinter so a great place
to learn Tkinter tricks!

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor