[Tutor] Globals as variables in a tkinter widget

2010-12-10 Thread echowit


This code produces a good run.
 
from tkinter import *
root = Tk()
class Application(Frame):
global sv, Ptype, PtypeI, sel_rate_label, label
label = Label(root)
Ptype = 999
PtypeI = IntVar()
W = 5
Y = 4
def sel(self):
global W
global Y
Ptype = PtypeI.get()
if Ptype <= 333: print('\nResulting Values:', PtypeI.get(),Ptype)
else: print('Ptype Failure:',PtypeI.get())
print('Ptype Completed',Ptype,'\n')
V = 2
X = 3
Z = X * 2
W = X + 6
Y = V ** 2
U = W * Y
print("V: ",V," X: ",X," Z: ",Z," W: ",W," Y: ",Y," U: ",U,'\n')
return

def __init__(self,master=None):
Frame.__init__(self)
self.pack()
sel_rate_label = Label(self)
sel_rate_label.config(text = '\nSelect Goats Personality Type',)
sel_rate_label.pack(anchor=N)
MODES = [
("1 Below Average", 000), ("2 Average", 111),
("3 Above Average", 222), ("4 Super Star", 333),
]
for text, mode in MODES:
b = Radiobutton(self, text=text,
variable=PtypeI, value=mode, command = self.sel)
b.pack(anchor=W)
label.pack()
return
app = Application(master=root) 
app.mainloop()
#root.destroy()

This is the IDLE output.
 
>>>  RESTART 
>>> 
Resulting Value: 333 333 
Ptype Completed 333 
V:  2  X:  3  Z:  6  W:  9  Y:  4  U:  36 
>>> 

Assigning U prior to recalculating W & Y:
 
U = W * Y
V = 2
X = 3
Z = X * 2
W = X + 6
Y = V ** 2
print("V: ",V," X: ",X," Z: ",Z," W: ",W," Y: ",Y," U: ",U,'\n')

Produces:

>>>  RESTART 
>>> 
Resulting Values: 333 333
Ptype Completed 333 
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python31\lib\tkinter\__init__.py", line 1399, in __call__
return self.func(*args)
  File "C:\Larry\FCGCF\GPDB\Dev\EX_OK.py", line 18, in sel
U = W * Y
TypeError: can't multiply sequence by non-int of type 'str'

My goal is to I learnto use a global variable as an argument inside a widget 
function,

My Question is: Anybody know of some good on-line documentation about using 
GUIs to do more than say 'Hello World'? 

Thanx in advance,

Larry Rochester
AKA echo...@aol.com


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


Re: [Tutor] Tutor Digest, Vol 82, Issue 45, Topic 3, Globals

2010-12-10 Thread echowit






-Original Message-
From: tutor-request 
To: tutor 
Sent: Fri, Dec 10, 2010 6:33 pm
Subject: Tutor Digest, Vol 82, Issue 45


Send Tutor mailing list submissions to
tutor@python.org
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.python.org/mailman/listinfo/tutor
r, via email, send a message with subject or body 'help' to
tutor-requ...@python.org
You can reach the person managing the list at
tutor-ow...@python.org
When replying, please edit your Subject line so it is more specific
han "Re: Contents of Tutor digest..."

oday's Topics:
   1. Re: role playing game - help needed (ALAN GAULD)
  2. Re: Writing to the terminal? (Alan Gauld)
  3. Re: Globals as variables in a tkinter widget (Alan Gauld)
  4. Re: the "**" operator? (Alex Hall)
  5. Code evaluation inside of string fails with __get_item
 (Tim Johnson)

-
Message: 1
ate: Fri, 10 Dec 2010 23:21:22 + (GMT)
rom: ALAN GAULD 
o: Al Stern 
c: "tutor@python.org" 
ubject: Re: [Tutor] role playing game - help needed
essage-ID: <448643.14907...@web86708.mail.ird.yahoo.com>
ontent-Type: text/plain; charset="utf-8"

Thanks again Alan.  Much clearer now.  One final part I don't understand.
 
>>> "%d is the result of %d + %d" % (6+7,6,7)
 
I understand (I think) the 6+7 part but why the ,6,7 after that.  I could 
see how either '6+7' or '6,7' would be the correct format but not both.
The format string has 3 percent markers - all numbers.
t is therefore expecting 3 values, so I have to give it them.
he first marker gets 6+7, ie 13. The second gets 6 
he third 7...
Look at the first example in the loops topic in my tutorial.
t uses variables in the same way rather than literal values, 
hat might make it more obvious. Maybe :-)
HTH,
Alan G.
- next part --
n HTML attachment was scrubbed...
RL: 

--
Message: 2
ate: Fri, 10 Dec 2010 23:42:56 -
rom: "Alan Gauld" 
o: tutor@python.org
ubject: Re: [Tutor] Writing to the terminal?
essage-ID: 
ontent-Type: text/plain; format=flowed; charset="iso-8859-1";
reply-type=original

Modulok"  wrote
> Assume I'm working in a command shell on a terminal. Something like
 tcsh on xterm, for example. I have a program which does *something*.
 Let's say it counts down from 10. How do I print a value, and then
 erase that value, replacing it with another value?
This is one of those things that sounds like it should be easy but is 
n
act deceptively difficult to get right!
It all  depends on your terminal, and there are dozens of different 
erminal
ypes and settings. Thats why Unix has a terminfo (or in some Unices
ermcap - even they can't agree! :-) library to define the different 
erminal
ypes and try to bring some sanity to proceedings.But you need to
earn the basic terminal control codes.
In this case you need the backspace code which is Ctrl-H or chr(8).
Try this
>>> print "spam" + "chr(8)*2+"ear"
If your terminal recognises chr(8) as backspace you should
ee "spear". If it doesn't you'll see someting like:
spam^h^hear
The only foolproof way to do this kind of thing is to use a library 
ike
urses which allows you to positioon the cursor and overwrite whatever
as there. But curses is non-trivial to use - not ridiculously hard, 
ut
on trivial.
You can get a basic intro to curses in the event-driven topic
f my tutorial.
BTW there is quite a good writeup on terminfo here:
ttp://tldp.org/HOWTO/Text-Terminal-HOWTO-16.html
nd, as usual, Wikipedia has a fair writeup.
-- 
lan Gauld
uthor of the Learn to Program web site
ttp://www.alan-g.me.uk/


-
Message: 3
ate: Sat, 11 Dec 2010 00:06:19 -
rom: "Alan Gauld" 
o: tutor@python.org
ubject: Re: [Tutor] Globals as variables in a tkinter widget
essage-ID: 
ontent-Type: text/plain; format=flowed; charset="iso-8859-1";
reply-type=original

echo...@aol.com> wrote
> This code produces a good run.
Not sure what you mean by that. What is "a good run"?
> from tkinter import *
 root = Tk()
 class Application(Frame):
 global sv, Ptype, PtypeI, sel_rate_label, label
This is bizarre.
lobals are generally considered evil and to be avoided if possible.
ne way to avoid globals is to use classes.
o why on earth are you trying to introduce globals ionto a class?
hat do you think you will achieve that using class variables can't?
> label = Label(root)
 Ptype = 999
PtypeI = IntVar()
W = 5
 Y = 4
And in fact most of these should probably be instance
ariables defined within your init() method...

   def sel(self):
   global W
   global Y
   Ptype = PtypeI.get()
   if Ptype <= 333: print('\nResulting Values:', 
typeI.get(),Ptype)
   else: print('Ptype Failure:',PtypeI.get())
   print('Ptype Completed',Ptype,'\n')
   V = 2
   X = 3
   Z = X * 2
   W = X + 6
   Y = V ** 2
  

Re: [Tutor] Tutor Digest, Vol 83, Issue 115

2011-01-26 Thread echowit





I not offering  a program design answer.  I don't have a clue as to your 
operation, but I've been there as far as QA software, CNC programs and Quality 
record keeping.

My thought is that instead of starting with the casting, think of the BOM on 
the final ass'y/configuration print and work backwards.

Hint, I wish I'd have had Python's Dictionary & Tuple when I was active.  






-Original Message-
From: tutor-request 
To: tutor 
Sent: Wed, Jan 26, 2011 6:55 am
Subject: Tutor Digest, Vol 83, Issue 115


Send Tutor mailing list submissions to
tutor@python.org
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.python.org/mailman/listinfo/tutor
r, via email, send a message with subject or body 'help' to
tutor-requ...@python.org
You can reach the person managing the list at
tutor-ow...@python.org
When replying, please edit your Subject line so it is more specific
han "Re: Contents of Tutor digest..."

oday's Topics:
   1. Re: class question (Alan Gauld)
  2. Re: Shuts down when asking user for info (Alan Gauld)
  3. Re: Shuts down when asking user for info (Alan Gauld)
  4. Re: class question (Karim)
  5. Re: class question (Karim)
  6. Re: class question (Elwin Estle)

-
Message: 1
ate: Wed, 26 Jan 2011 11:08:07 -
rom: "Alan Gauld" 
o: tutor@python.org
ubject: Re: [Tutor] class question
essage-ID: 
ontent-Type: text/plain; format=flowed; charset="iso-8859-1";
reply-type=response

Karim"  wrote
> Program towards interface that means you have to use inheritance.
Just to be picky, you can program by interface without using 
nheritance. Inheritance is only needed to implement interfaces 
n languages like C++. In Python (and other dynamically 
ound OOP languages) you can use polymorphism without 
nheritance.
-- 
lan Gauld
uthor of the Learn to Program web site
ttp://www.alan-g.me.uk/


-
Message: 2
ate: Wed, 26 Jan 2011 11:13:59 -
rom: "Alan Gauld" 
o: tutor@python.org
ubject: Re: [Tutor] Shuts down when asking user for info
essage-ID: 
ontent-Type: text/plain; format=flowed; charset="iso-8859-1";
reply-type=original

Casey Key"  wrote
> Please help , run the script and see that once you press enter after
 entering name it just shuts it down, help.
> --
rint("\tWelcome to Casey's magical python code!")
rint("\tWritten by no other than Casey Key!")
rint("\nWe will begin by getting some information,mmmkay?")
ame = input("what is your name")
rint("your name is",name)
-
Python is case sensitive, print starts with a lowercase P.
However that should have generated an error.
ow did you run the program? If you ran it from an IDE or
rom the command prompt you would have seen the error.
 suspect you are just double clicking in a file manager?
oing that the window will close before you can read it.
lways test your code by running from a prompt so that
ou see all output, including errors.

lan Gauld
uthor of the Learn To Program website
ttp://www.alan-g.me.uk/




-
Message: 3
ate: Wed, 26 Jan 2011 11:09:35 -
rom: "Alan Gauld" 
o: tutor@python.org
ubject: Re: [Tutor] Shuts down when asking user for info
essage-ID: 
ontent-Type: text/plain; format=flowed; charset="iso-8859-1";
reply-type=original

tee chwee liong"  wrote
> maybe you can use raw_input?
It looks like the OP is using Python 3 so input() is the correct 
unction.

lan G. 


-
Message: 4
ate: Wed, 26 Jan 2011 12:19:43 +0100
rom: Karim 
o: tutor@python.org
ubject: Re: [Tutor] class question
essage-ID: <4d40034f.4080...@free.fr>
ontent-Type: text/plain; charset=ISO-8859-1; format=flowed

ure, but I come from java world and 1 inheritance is allowed but we can 
mplement multiple interfaces.
 like to use Abstract classes in Python then inherit and implement 
bstract method like in java (at least
hat's all I remember from Java experience).
ndeed I was too direct everybody has the same style.
Regards
arim
On 01/26/2011 12:08 PM, Alan Gauld wrote:

 "Karim"  wrote

> Program towards interface that means you have to use inheritance.

 Just to be picky, you can program by interface without using 
 inheritance. Inheritance is only needed to implement interfaces in 
 languages like C++. In Python (and other dynamically bound OOP 
 languages) you can use polymorphism without inheritance.


--
Message: 5
ate: Wed, 26 Jan 2011 12:25:19 +0100
rom: Karim 
o: Steven D'Aprano 
c: tutor@python.org
ubject: Re: [Tutor] class question
essage-ID: <4d40049f.5050...@free.fr>
ontent-Type: text/plain; charset=ISO-8859-1; format=flowed

 know the the law of Murphy.
ut this one is a must-have.:-)
Regards
arim
> * One way to reduce coupling is with the Law of Demeter: if you want 
 your dog to walk, don't talk to your dog's legs. You will only confuse 
 the dog and it won't get anywhere.

 http://en.wikipedia.org/wiki/Law_of_Demeter



 Hope thi