473,465 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to make a widget print itself?

TMS
119 New Member
BartonC has helped me with this on another thread, but I've had to start over, and have a new question, and have had a nice spring break to calm down a bit.

I can't seem to get the widget to print the value of itself. Right now, it prints an 'A' just for a test. I've tried calling self.keyButton and simply self, but self returns an address (I think it looks like this: .12854776) and some other things as well, but they don't work for me. Here is the code:

Expand|Select|Wrap|Line Numbers
  1. buttonData = \
  2. (
  3. ( (' ~ \n ` \n ', 1), (' ! \n 1 \n ', 1), (' @ \n 2 \n', 1), (' # \n 3 \n', 1), (' $ \n 4 \n', 1), 
  4. (' % \n 5 \n', 1), (' ^ \n 6 \n', 1), (' & \n 7 \n', 1), (' * \n 8 \n', 1), (' ( \n 9 \n', 1), 
  5. (' ) \n 0 \n', 1), (' _ \n - \n', 1), (' + \n = \n', 1), (' \n    Backspace    \n', 6), (), ()),
  6. ((' Tab \n', 2), (), (' Q \n', 1), (' W \n', 1), (' E \n', 1), (' R \n', 1), (' T \n', 1),
  7. (' Y \n', 1), (' U \n ', 1), (' I \n ', 1), (' O \n ', 1), (' P \n ', 1), (' { \n [ ', 1), 
  8. ('    } \n    ] ', 1 ), ('     |     \n \\ ', 1) ),
  9. (('Caps Lock\n', 3), (), (), ('A\n', 1), ('S\n', 1), ('D\n', 1), ('F\n', 1), ('G\n', 1),
  10. ('H\n', 1), ('J\n', 1), ('K\n', 1), ('L\n', 1), (':\n;', 1), ('"\n\'', 1),
  11. ('Enter\n ', 2)),
  12. (('Shift\n', 3), (), (), ('Z\n', 1), ('X\n', 1), ('C\n', 1), ('V\n', 1), ('B\n', 1),
  13. ('N\n', 1), ('M\n', 1), ('<\n,', 1), ('>\n.', 1), ('?\n/', 1), ('Shift\n', 2)),
  14. (('\nCtrl', 3), (), (), ('\nAlt', 2), (), ('\n         Space             ', 7), (), (),(),(),(), (), ('\nAlt', 2), (), ('\nCtrl', 2)),
  15. )
  16. from Tkinter import *
  17. #from buttonLayout import * #don't need this if its on one page
  18. class keyboard(Frame):
  19.     def __init__(self, master):
  20.         Frame.__init__(self, master)
  21.         self.grid()
  22.         self.create_widgets()
  23.     def create_widgets(self):
  24.         for rowNum, rowTuple in enumerate(buttonData):
  25.             for colNum, constTuple in enumerate(rowTuple):
  26.                 if constTuple:
  27.                     self.keyButton=Button(self, text = constTuple[0], command=self.DoAction)
  28.                     self.keyButton.grid(row=rowNum, column=colNum, columnspan = constTuple[1], sticky=N+E+S+W)
  29.  
  30.     def DoAction(self):
  31.         try:
  32.             print 'A', #tried print self.keyButton, but doesn't work
  33.         except:
  34.             print "nope"
  35.     def state(self, state): #haven't really used this yet
  36.         self.state=state
  37. if __name__ == '__main__':
  38.     root=Tk()
  39.     root.title("K E Y B O A R D")
  40.     myKeyboard = keyboard(root)
  41.     root.mainloop()
  42.  
  43.  
signed....

still learning :)
Mar 19 '07 #1
8 1249
bartonc
6,596 Recognized Expert Expert
Ok, Still Learning. You can call me Barton.
What I'm about to give you will take some study of the Tkinter classes (it's faily advanced stuff). This is event handling at its finest in Tkinter:
Expand|Select|Wrap|Line Numbers
  1. buttonData = \
  2. (
  3. ( (' ~ \n ` \n ', 1), (' ! \n 1 \n ', 1), (' @ \n 2 \n', 1), (' # \n 3 \n', 1), (' $ \n 4 \n', 1),
  4. (' % \n 5 \n', 1), (' ^ \n 6 \n', 1), (' & \n 7 \n', 1), (' * \n 8 \n', 1), (' ( \n 9 \n', 1),
  5. (' ) \n 0 \n', 1), (' _ \n - \n', 1), (' + \n = \n', 1), (' \n  Backspace       \n', 6), (), ()),
  6. ((' Tab \n', 2), (), (' Q \n', 1), (' W \n', 1), (' E \n', 1), (' R \n', 1), (' T \n', 1),
  7. (' Y \n', 1), (' U \n ', 1), (' I \n ', 1), (' O \n ', 1), (' P \n ', 1), (' { \n [ ', 1),
  8. ('      } \n    ] ', 1 ), ('     |       \n \\ ', 1) ),
  9. (('Caps Lock\n', 3), (), (), ('A\n', 1), ('S\n', 1), ('D\n', 1), ('F\n', 1), ('G\n', 1),
  10. ('H\n', 1), ('J\n', 1), ('K\n', 1), ('L\n', 1), (':\n;', 1), ('"\n\'', 1),
  11. ('Enter\n ', 2)),
  12. (('Shift\n', 3), (), (), ('Z\n', 1), ('X\n', 1), ('C\n', 1), ('V\n', 1), ('B\n', 1),
  13. ('N\n', 1), ('M\n', 1), ('<\n,', 1), ('>\n.', 1), ('?\n/', 1), ('Shift\n', 2)),
  14. (('\nCtrl', 3), (), (), ('\nAlt', 2), (), ('\n           Space                   ', 7), (), (),(),(),(), (), ('\nAlt', 2), (), ('\nCtrl', 2)),
  15. )
  16.  
  17.  
  18. from Tkinter import *
  19. #from buttonLayout import * #don't need this if its on one page
  20.  
  21.  
  22. class keyboard(Frame):
  23.     def __init__(self, master):
  24.         Frame.__init__(self, master)
  25.         self.grid()
  26.         self.create_widgets()
  27.         self.state = 0
  28.  
  29.     def create_widgets(self):
  30.         for rowNum, rowTuple in enumerate(buttonData):
  31.             for colNum, constTuple in enumerate(rowTuple):
  32.                 if constTuple:
  33.                     keyButton=Button(self, text=constTuple[0])
  34.                     keyButton.bind("<Button-1>", self.DoAction)
  35.                     keyButton.grid(row=rowNum, column=colNum, columnspan = constTuple[1], sticky=N+E+S+W)
  36.  
  37.     def DoAction(self, event):
  38.         """Get the widget from the event, then get the 'text' attribute of the widget."""
  39.         # You'll need a very consistent labeling scheme or lots of "if textItem[1] == "Space":
  40.         # type of tests.
  41.         widgetText = event.widget['text']
  42.         print widgetText
  43.         textItems = widgetText.split()
  44.         print textItems
  45.         print textItems[self.state]
  46.  
  47.     def state(self, state): #haven't really used this yet
  48.         self.state=state
  49.  
  50.  
  51.  
  52.  
  53. if __name__ == '__main__':
  54.     root=Tk()
  55.     root.title("K E Y B O A R D")
  56.     myKeyboard = keyboard(root)
  57.     root.mainloop()
  58.  
Nice job on the keyboard layout by the way!
Mar 20 '07 #2
bvdet
2,851 Recognized Expert Moderator Specialist
Ok, Still Learning. You can call me Barton.
What I'm about to give you will take some study of the Tkinter classes (it's faily advanced stuff). This is event handling at its finest in Tkinter:
Expand|Select|Wrap|Line Numbers
  1. buttonData = \
  2. (
  3. ( (' ~ \n ` \n ', 1), (' ! \n 1 \n ', 1), (' @ \n 2 \n', 1), (' # \n 3 \n', 1), (' $ \n 4 \n', 1),
  4. (' % \n 5 \n', 1), (' ^ \n 6 \n', 1), (' & \n 7 \n', 1), (' * \n 8 \n', 1), (' ( \n 9 \n', 1),
  5. (' ) \n 0 \n', 1), (' _ \n - \n', 1), (' + \n = \n', 1), (' \n  Backspace       \n', 6), (), ()),
  6. ((' Tab \n', 2), (), (' Q \n', 1), (' W \n', 1), (' E \n', 1), (' R \n', 1), (' T \n', 1),
  7. (' Y \n', 1), (' U \n ', 1), (' I \n ', 1), (' O \n ', 1), (' P \n ', 1), (' { \n [ ', 1),
  8. ('      } \n    ] ', 1 ), ('     |       \n \\ ', 1) ),
  9. (('Caps Lock\n', 3), (), (), ('A\n', 1), ('S\n', 1), ('D\n', 1), ('F\n', 1), ('G\n', 1),
  10. ('H\n', 1), ('J\n', 1), ('K\n', 1), ('L\n', 1), (':\n;', 1), ('"\n\'', 1),
  11. ('Enter\n ', 2)),
  12. (('Shift\n', 3), (), (), ('Z\n', 1), ('X\n', 1), ('C\n', 1), ('V\n', 1), ('B\n', 1),
  13. ('N\n', 1), ('M\n', 1), ('<\n,', 1), ('>\n.', 1), ('?\n/', 1), ('Shift\n', 2)),
  14. (('\nCtrl', 3), (), (), ('\nAlt', 2), (), ('\n           Space                   ', 7), (), (),(),(),(), (), ('\nAlt', 2), (), ('\nCtrl', 2)),
  15. )
  16.  
  17.  
  18. from Tkinter import *
  19. #from buttonLayout import * #don't need this if its on one page
  20.  
  21.  
  22. class keyboard(Frame):
  23.     def __init__(self, master):
  24.         Frame.__init__(self, master)
  25.         self.grid()
  26.         self.create_widgets()
  27.         self.state = 0
  28.  
  29.     def create_widgets(self):
  30.         for rowNum, rowTuple in enumerate(buttonData):
  31.             for colNum, constTuple in enumerate(rowTuple):
  32.                 if constTuple:
  33.                     keyButton=Button(self, text=constTuple[0])
  34.                     keyButton.bind("<Button-1>", self.DoAction)
  35.                     keyButton.grid(row=rowNum, column=colNum, columnspan = constTuple[1], sticky=N+E+S+W)
  36.  
  37.     def DoAction(self, event):
  38.         """Get the widget from the event, then get the 'text' attribute of the widget."""
  39.         # You'll need a very consistent labeling scheme or lots of "if textItem[1] == "Space":
  40.         # type of tests.
  41.         widgetText = event.widget['text']
  42.         print widgetText
  43.         textItems = widgetText.split()
  44.         print textItems
  45.         print textItems[self.state]
  46.  
  47.     def state(self, state): #haven't really used this yet
  48.         self.state=state
  49.  
  50.  
  51.  
  52.  
  53. if __name__ == '__main__':
  54.     root=Tk()
  55.     root.title("K E Y B O A R D")
  56.     myKeyboard = keyboard(root)
  57.     root.mainloop()
  58.  
Nice job on the keyboard layout by the way!
That's really neat! Good job guys.
Mar 20 '07 #3
TMS
119 New Member
That is great. We haven't gone over events, but I've been reading about them in the Lundh documentation. That looks great!

Thank you again!

TMS
Mar 20 '07 #4
TMS
119 New Member
Guess what? It isn't what the teacher wanted. He wanted me to use a callback. I'm not real familiar with callbacks, he kinda glossed over it in class, and the info on the internet isn't real clear to me.

He had me change the code like this:

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. from buttonLayout import *
  3.  
  4. class keyboard(Frame):
  5.     def __init__(self, master, callback):
  6.         Frame.__init__(self, master)
  7.         self.grid()
  8.         self.create_widgets()
  9.         self.index = 0
  10.         self.callback = callback 
  11.     def create_widgets(self):
  12.         for rowNum, rowTuple in enumerate(buttonData):
  13.             for colNum, constTuple in enumerate(rowTuple):
  14.                 if constTuple:
  15.                     keyButton=Button(self, text=constTuple[0])
  16.                     keyButton.bind("<Button-1>", self.DoAction)
  17.                     keyButton.grid(row=rowNum, column=colNum, columnspan = constTuple[1], sticky=N+E+S+W)
  18.     def DoAction(self, event):
  19.         """Get the widget from the event, then get the 'text' attribute of the widget."""
  20.         # lots of "if textItem[1] == "Space": type of tests.
  21.         widgetText = event.widget['text']
  22.         #print widgetText,
  23.         textItems = widgetText.split()
  24.         if textItems[self.index] in ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
  25.             lower(textItems[self.index])  #this is for normal state
  26.             # I know the following is wrong it prints the address of the function:
  27.             self.callback(func)  #this is where I need help
  28.     def index(self, index): 
  29.         self.index=index
  30.  
  31.  
  32. if __name__ == '__main__':
  33.     root=Tk()
  34.     root.title("K E Y B O A R D")
  35.     def func(key):
  36.         print key,
  37.     myKeyboard = keyboard(root, func)
  38.     root.mainloop()
  39.  
  40.  
So, my question now is how do I use the callback to call the function in main? Or am I thinking about it wrong? I have to call the func somewhere.

TMS
Mar 21 '07 #5
TMS
119 New Member
LOL, nevermind. I found it.


Expand|Select|Wrap|Line Numbers
  1. if textItems[self.index] in ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
  2.             textItems = lower(textItems[self.index])  #this is for normal state
  3.             self.callback(textItems[self.index])  
  4.  
I just needed to think about it a little longer, I suppose.
Mar 21 '07 #6
bartonc
6,596 Recognized Expert Expert
Guess what? It isn't what the teacher wanted. He wanted me to use a callback. I'm not real familiar with callbacks, he kinda glossed over it in class, and the info on the internet isn't real clear to me.

He had me change the code like this:

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. from buttonLayout import *
  3.  
  4. class keyboard(Frame):
  5.     def __init__(self, master, callback):
  6.         Frame.__init__(self, master)
  7.         self.grid()
  8.         self.create_widgets()
  9.         self.index = 0
  10.         self.callback = callback 
  11.     def create_widgets(self):
  12.         for rowNum, rowTuple in enumerate(buttonData):
  13.             for colNum, constTuple in enumerate(rowTuple):
  14.                 if constTuple:
  15.                     keyButton=Button(self, text=constTuple[0])
  16.                     keyButton.bind("<Button-1>", self.DoAction)
  17.                     keyButton.grid(row=rowNum, column=colNum, columnspan = constTuple[1], sticky=N+E+S+W)
  18.     def DoAction(self, event):
  19.         """Get the widget from the event, then get the 'text' attribute of the widget."""
  20.         # lots of "if textItem[1] == "Space": type of tests.
  21.         widgetText = event.widget['text']
  22.         #print widgetText,
  23.         textItems = widgetText.split()
  24.         if textItems[self.index] in ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
  25.             lower(textItems[self.index])  #this is for normal state
  26.             # I know the following is wrong it prints the address of the function:
  27.             self.callback(func)  #this is where I need help
  28.     def index(self, index): 
  29.         self.index=index
  30.  
  31.  
  32. if __name__ == '__main__':
  33.     root=Tk()
  34.     root.title("K E Y B O A R D")
  35.     def func(key):
  36.         print key,
  37.     myKeyboard = keyboard(root, func)
  38.     root.mainloop()
  39.  
  40.  
So, my question now is how do I use the callback to call the function in main? Or am I thinking about it wrong? I have to call the func somewhere.

TMS
Now you teacher is really starting to bug me. The line
Expand|Select|Wrap|Line Numbers
  1. self.keyButton=Button(self, text = constTuple[0], command=self.DoAction)
specifies DoAction() as the callback function. There are advanced techniques for getting this callback to take arguments, but I'll bet that's not what the grinch is after either. I still like the subclass of Button from the early version as the best way to do this without using event handling. That subclass used callback in the manner that you are after. Let's see what you can come up with. And have fun!
Mar 21 '07 #7
TMS
119 New Member
LOL, yeah, I'm really having fun now.

OK, this is for the win. I have to figure out the states. I was given some pseudocode that goes like this:

for CapsLock key:
if the keyboard state is cap:
set the state to normal
otherwise:
set the state to cap

For the Shift key:
if the keyboard state is shifted or cap:
set the state to normal
otherwise:
set the state to shifted

normal state means that keys are lower case, in shifted and cap states keys are upper case.

dual symbol:
in normal and cap dual symbol keys pass lower symbol (index +1). In shifted state they pass upper symbol.

special keys like space, alt, ctrl, back space, tab just print themselves. (I had actually made them do what they were supposed to do, but that isn't what he wanted). Shift and Caps Lock change the state of the keyboard based on the above pseudocode.

SO you brilliant people, for the win: I've been trying to write a function for normal and it isn't working very well. Here is what I have, can you point out what my problem is? I'm thinking I can write a function for each state, but since I can't get the normal function to work, figured I had better get some help on that. Unfortunately, this is due tonight. I do appreciate whatever help I can get, but I really don't think it will be finished before class.

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. from buttonLayout import *
  3. from string import *
  4. #from layout import *
  5.  
  6. class keyboard(Frame):
  7.     def __init__(self, master, callback):
  8.         Frame.__init__(self, master)
  9.         self.create_widgets()
  10.         self.index = 0
  11.         self.callback = callback
  12.     def create_widgets(self):
  13.         for rowNum, rowTuple in enumerate(buttonData):
  14.             for colNum, constTuple in enumerate(rowTuple):
  15.                 if constTuple:
  16.                     keyButton=Button(self, text=constTuple[0])
  17.                     keyButton.bind("<Button-1>", self.DoAction)
  18.                     keyButton.grid(row=rowNum, column=colNum, columnspan = constTuple[1], sticky=N+E+S+W)
  19.     def DoAction(self, event):
  20.         """Get the widget from the event, then get the 'text' attribute of the widget."""
  21.         # lots of "if textItem[1] == "Space": type of tests.
  22.         widgetText = event.widget['text']
  23.         textItems = widgetText.split()
  24.         if textItems[self.index] == 'Shift':
  25.             #I don't want shift to print... this is actually shifted state which should have another function
  26.             self.callback(textItems[self.index])
  27.         else:
  28.             norm(textItems[self.index]) #error message says norm isn't defined. 
  29.     def norm(self, event):
  30.         self.event = event
  31.         if textItems[self.index] in ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
  32.                 textItems = lower(textItems[self.index]) 
  33.                 self.callback(textItems[self.index])
  34.         elif textItems[self.index] in ('~!@#$%^&*()_+{}|:"<>?'):
  35.                 textItems = textItems[self.index+1]
  36.                 self.callback(textItems[self.index])
  37.         elif textItems[self.index] in ("'Space','Ctrl','Alt','Enter','Backspace', 'Tab'"):
  38.                 self.callback(textItems[self.index])
  39.  
  40.     def index(self, index): 
  41.         self.index=index
  42.  
  43. if __name__ == '__main__':
  44.     root=Tk()
  45.     root.title("K E Y B O A R D")
  46.     def func(key):
  47.         print key,
  48.     myKeyboard = keyboard(root, func)
  49.     myKeyboard.grid()
  50.     root.mainloop()
  51.  
  52.  
TMS
Mar 21 '07 #8
bartonc
6,596 Recognized Expert Expert
(I had actually made them do what they were supposed to do, but that isn't what he wanted).
I really disagree with you teacher on this. A programmers first job is to make a program that works. The second job is to make a program that is clean and easy to understand, which is a lot easier to do AFTER you have working code.
I'm off to bible study tonight, so I'll have to look at the code later.
Mar 21 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Elaine Jackson | last post by:
I've got a script where a button gets pushed over and over: to cut down on the carpal tunnel syndrome I'd like to have the button respond to presses of the Enter key as well as mouse clicks; can...
4
by: Clara | last post by:
Hi, can somebody help me,..I have an assignment due next week but now I'm stuck with this problem.... I tried to get values from entry widget using the widgetcontrolvariable.get(),..but it seems...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
2
by: Yi | last post by:
I want to make a simple javascript widget, something looks like the Google AdWords, that people can just post a small section of code on their web page and display some content from my website. ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.