By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,715 Members | 768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,715 IT Pros & Developers. It's quick & easy.

Tkinter, and calling other classes

P: 18
Hi,
i'm kind of new at programming, so i was just wandering if anyone can help me with a little problem that i've been having for a couple of hours now.
i have a piece of code that's was given to me like this and can't be touched:
----------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. DIGITS=4
  2. class EntryFrame(Frame):
  3.     """A widget base class for entering a list of digits
  4.  
  5.     Constructor: EntryFrame(parent, num) - parent is the parent widget
  6.     and num is the number of digits to be entered.
  7.     """
  8.  
  9. ----def __init__(self, parent, num):
  10. --------Frame.__init__(self, parent)
  11. --------self.entries = []
  12. --------self.num = num
  13. --------for i in range(num):
  14. ------------entry =  Entry(self, width=1, font=GUESSFONT)
  15. ------------entry.pack(side=LEFT,padx=0)
  16. ------------entry.bind("<Key>", self.keyevent)
  17. ------------self.entries.append(entry)
  18. --------self.entries[0].focus()
  19. --------self.ok = Button(self, text="Guess", command=self.guess)
  20. --------self.ok.pack(side=LEFT, padx=10)
  21. --------self.cancel = Button(self, text="Clear", command=self.clear)
  22. --------self.cancel.pack(side=LEFT, padx=10)
  23. --------self.statustext = StringVar()
  24. --------Label(self, textvariable=self.statustext,\
  25. --------------anchor=W,width=30).pack(side=LEFT)
  26. --------self.setStatus()
------------------------------------------------------------------
i also have a master class called controller. the problem is in the controller class i want to call the entryframe class so that when i run the code it will create what the code above is going to create, but i don't know how to do that.
i've been trying EntryFrame.__init__(parent, DIGITS)
but i keep getting the error global name parent is not defined.
i don't know what to do now.
please help.
thanks
phoenix1990
May 12 '09 #1
Share this Question
Share on Google+
17 Replies


boxfish
Expert 100+
P: 469
If you don't want to specify a parent, make an EntryFrame object like this:
Expand|Select|Wrap|Line Numbers
  1. self.myEntryFrame = EntryFrame(None, DIGITS)
I hope this is helpful.
May 12 '09 #2

P: 18
@boxfish
thank you, boxfish for replying. however that seems to have no effect on it whatsoever.
i can specify a parent in another class if i want to, but i'm not sure how to do that. again i'm still a noob when it comes to programming.
May 12 '09 #3

boxfish
Expert 100+
P: 469
So do you want to create an EntryFrame object as a member in your Controller class? Here is how you would do that:

Expand|Select|Wrap|Line Numbers
  1. # Put stuff for EntryFrame class up here
  2.  
  3. class Controller(object):
  4.     def __init__(self):
  5.         self.myEntryFrame = EntryFrame(None, DIGITS)
  6.  
  7. myController = Controller()
Let me know if this is not what you're after or if you're having a problem with it.
May 13 '09 #4

P: 18
@boxfish

yeah it turns out that the code you gave me earlier did work, i just didn't pack it into the frame.
thanks for you help boxfish
May 14 '09 #5

P: 18
just one more question, and it quite a long one, but don't understand how inheritence works much

so here's the full code of the EntryFrame class
[code]
Expand|Select|Wrap|Line Numbers
  1. DIGITS=4
  2. class EntryFrame(Frame):
  3.     """A widget base class for entering a list of digits
  4.  
  5.     Constructor: EntryFrame(parent, num) - parent is the parent widget
  6.     and num is the number of digits to be entered.
  7.     """
  8.  
  9. ----def __init__(self, parent, num):
  10. --------Frame.__init__(self, parent)
  11. --------self.entries = []
  12. --------self.num = num
  13. --------for i in range(num):
  14. ------------entry =  Entry(self, width=1, font=GUESSFONT)
  15. ------------entry.pack(side=LEFT,padx=0)
  16. ------------entry.bind("<Key>", self.keyevent)
  17. ------------self.entries.append(entry)
  18. --------self.entries[0].focus()
  19. --------self.ok = Button(self, text="Guess", command=self.guess)
  20. --------self.ok.pack(side=LEFT, padx=10)
  21. --------self.cancel = Button(self, text="Clear", command=self.clear)
  22. --------self.cancel.pack(side=LEFT, padx=10)
  23. --------self.statustext = StringVar()
  24. --------Label(self, textvariable=self.statustext,\
  25. --------------anchor=W,width=30).pack(side=LEFT)
  26. --------self.setStatus()
  27.  
  28.     def keyevent(self, e):
  29.         """The key event callback."""
  30.  
  31.         index = self.entries.index(e.widget)
  32.         v = e.char
  33.         sym = e.keysym
  34.         if sym == 'Right':              # rightarrow
  35.             index = (index+1)%self.num
  36.             self.entries[index].icursor(END)
  37.             self.entries[index].focus()
  38.             self.entries[index].select_range(0,END)
  39.         elif sym == 'Left':             # leftarrow
  40.             index = (index-1)%self.num
  41.             self.entries[index].icursor(END)
  42.             self.entries[index].focus()
  43.             self.entries[index].select_range(0,END)
  44.  
  45.         elif sym == 'BackSpace':  
  46.             if index > 0 and self.entries[index].get() == '':
  47.                 index -= 1
  48.                 self.entries[index].focus()
  49.                 self.entries[index].select_range(0,END)
  50.             self.after_idle(self.handleEntries, index)
  51.         elif sym == 'Return':           # enter
  52.             self.guess()
  53.         elif sym == "Delete":
  54.             self.clear()
  55.         elif v.isdigit():
  56.             self.entries[index].delete(0,END)
  57.             if index < self.num-1:
  58.                 index +=1
  59.             self.after_idle(self.handleEntries, index)
  60.             self.entries[index].focus()
  61.             self.entries[index].select_range(0,END)
  62.         elif v and (v.isalpha() or v in CHARS):
  63.             self.entries[index].delete(0,END)
  64.             self.handleEntries(index)
  65.             return "break"
  66.  
  67.     def clear(self):
  68.         """Clear all the entries."""
  69.  
  70.         for i in range(self.num):
  71.             self.entries[i].delete(0,END)
  72.             self.handleEntries(i)
  73.         self.entries[0].focus()
  74.         self.setStatus()
  75.  
  76.     def handleEntries(self, index):
  77.         """The after_idle callback - highlight the text in the current
  78.         entry and process the entries
  79.         """
  80.  
  81.         self.entries[index].select_range(0,END)
  82.         self.processEntries()
  83.  
  84.     def guess(self):
  85.         """Called when a guess is entered - to be overridden
  86.         in the subclass."""
  87.  
  88.         pass
  89.  
  90.     def processEntries(self):
  91.         """Called whenever a digit is added or deleted - to be overridden
  92.         in the subclass."""
  93.  
  94.         pass
  95.  
  96.     def setStatus(self):
  97.         """Set the text in the status label - - to be overridden
  98.         in the subclass."""
  99.  
  100.         self.statustext.set("Status:")
what i want to know is how do i go about creating another class which inherits from EntryFrame. and overrides the setstatus definition. if someone can show me how to just do the setstatus part i should be able to get the rest on my own. all it needs to do is say that if there are duplicate numbers entered, it would say 'Repeared Digits', but if there are no duplicates then it would say 'Ready' next to the word 'Status:'

sorry for asking soo much questions, but i really appreciate you help.
May 14 '09 #6

boxfish
Expert 100+
P: 469
Sorry for the long response time. You can just define a new setStatus function in your new class and it will override the old one. If you want to call the old one from the new one, do this:
Expand|Select|Wrap|Line Numbers
  1. EntryFrame.setStatus(self)
May 16 '09 #7

P: 18
@boxfish
is this what had. i think it's a little different to what you are saying i should do
Expand|Select|Wrap|Line Numbers
  1. class View(EntryFrame):
  2.     def __init__(self, parent, num):
  3.         EntryFrame.__init__(self, parent, num)
  4.         self.pack(anchor=W)
  5.     def setStatus(self):
  6.         n=0
  7.         for i in self.entries:
  8.             if i in self.entries[n+1:]: #not too sure on this part
  9.                 self.statustext.set('Status: Repeated Digits')
  10.             elif len(self.entries)==4:
  11.                 self.statustext.set('Status: Ready')
  12.             else:
  13.                 self.statustext.set('Status:')
  14.             n+=1
  15.  
that works a bit, but every time i type in a entry in the gui it doesn't show up with the proper status text, it just stays on the one text. So how do i go about making it so that the controller class will constantly update the gui according to the entries that were entered?
May 16 '09 #8

boxfish
Expert 100+
P: 469
I think you also need to override the processEntries function so it calls the setStatus function. As for the setStatus function, I think you are treating the list of entries like a list of strings. I suggest getting a list of strings from the entries then working with that. But then again, the entries can only contain digits, right? How about making a list of ints:
Expand|Select|Wrap|Line Numbers
  1. entryDigits = []
  2. for i in self.entries:
  3.     entryDigits.append(int(i.get()))
As for the repeated digits part, you have to finish checking for repeated digits before you decide what status to write. I know xrange(len(entryDigits)) is ugly, but this loop should check for repeated digits:
Expand|Select|Wrap|Line Numbers
  1. repeatedDigits = False
  2. for i in xrange(len(entryDigits)): # Loop through indices of entryDigits.
  3.     for j in entryDigits[i+1:]: # Loop through items in remaining part of entryDigits.
  4.         if j == entryDigits[i]: # Uh oh, repeated digit!
  5.             repeatedDigits = True # Set the flag.
  6.             break # Stop looking for repeated digits.
  7.     if repeatedDigits:
  8.         break # Make sure we stop looking for repeated digits.
I hope this is helpful. Let me know if it's not working.
May 16 '09 #9

P: 18
so i overrode the process entries to call the setStatus function, and everything seems to be working. however
Expand|Select|Wrap|Line Numbers
  1. entryDigits = []
  2. for i in self.entries:
  3.     entryDigits.append(int(i.get()))
won't work since every entry in self.entries is a string and each entry initally starts off as an empty string. so it can't turn an empty string into an integer. so i decided to remove the init and that seemed to work, but now it always says "Status: Repeated Digits" since the repeated digits code will initially see 4 lots of empty strings in the entryDigits list. the only time it changes is when all four entries have been filled in the GUI, and none of them are the same.
May 17 '09 #10

P: 18
nevermind i got it to work in the end, by adding this in
Expand|Select|Wrap|Line Numbers
  1.  
  2. entryDigits = []
  3. for i in self.entries:
  4.     entryDigits.append(i.get())
  5.     for i in entryDigits:
  6.         if i=='':
  7.             entryDigits.remove(i)
May 17 '09 #11

boxfish
Expert 100+
P: 469
While that code will work, it is doing a lot more work than it needs to. The fact that the for loops are nested means that the inner one is being executed over and over again. Try this:
Expand|Select|Wrap|Line Numbers
  1. entryDigits = []
  2. for i in self.entries:
  3.     if i.get():
  4.         entryDigits.append(int(i.get()))
I hope this is helpful.
May 17 '09 #12

P: 18
thanks for the help boxfish.
if i run into anymore problems i'll let you know
May 18 '09 #13

P: 18
ok so i've run into a problem with the code you gave me. when i click clear. it runs the clear definition that was defined in the entryframe class. however it will only delete the first entry in the GUI's frame and not the other three..
May 19 '09 #14

P: 18
@phoenix1990
nevermind, it fixed itself somehow.
May 19 '09 #15

P: 18
ok, so i've pretty much finished everything, but i'm having a little problem with creating a popup dialog box.
i've defined a new class called DialogBox which looks like this
Expand|Select|Wrap|Line Numbers
  1. class DiaglogBox:
  2.     def __init__(self, master):
  3.         if self.entryframe.win==True:
  4.             self.win()
  5.         else:
  6.             self.lose()
however every time i try to call that function(self.dialogbox=DialogBox(None)) in another class i keep getting the error.
global name 'DialogBox' is not defined
May 20 '09 #16

boxfish
Expert 100+
P: 469
DialogBox needs to inherit from the Frame class. Make it
Expand|Select|Wrap|Line Numbers
  1. class DialogBox(Frame):
Also, if it's a dialog box, you need to tell it which window is its parent. When you create a DialogBox object, pass it its parent window instead of None.
Let me know whether this works.
May 20 '09 #17

P: 18
@boxfish
IT WORKED!!! and i'm finally finished with all the stuff i needed to do. thanks for all your help during the past week boxfish. you saved me a lot of time and stress.
May 22 '09 #18

Post your reply

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