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:
---------------------------------------------------------- -
DIGITS=4
-
class EntryFrame(Frame):
-
"""A widget base class for entering a list of digits
-
-
Constructor: EntryFrame(parent, num) - parent is the parent widget
-
and num is the number of digits to be entered.
-
"""
-
-
----def __init__(self, parent, num):
-
--------Frame.__init__(self, parent)
-
--------self.entries = []
-
--------self.num = num
-
--------for i in range(num):
-
------------entry = Entry(self, width=1, font=GUESSFONT)
-
------------entry.pack(side=LEFT,padx=0)
-
------------entry.bind("<Key>", self.keyevent)
-
------------self.entries.append(entry)
-
--------self.entries[0].focus()
-
--------self.ok = Button(self, text="Guess", command=self.guess)
-
--------self.ok.pack(side=LEFT, padx=10)
-
--------self.cancel = Button(self, text="Clear", command=self.clear)
-
--------self.cancel.pack(side=LEFT, padx=10)
-
--------self.statustext = StringVar()
-
--------Label(self, textvariable=self.statustext,\
-
--------------anchor=W,width=30).pack(side=LEFT)
-
--------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.__in it__(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
17 5777 boxfish 469
Recognized Expert Contributor
If you don't want to specify a parent, make an EntryFrame object like this: - self.myEntryFrame = EntryFrame(None, DIGITS)
I hope this is helpful.
@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.
boxfish 469
Recognized Expert Contributor
So do you want to create an EntryFrame object as a member in your Controller class? Here is how you would do that: - # Put stuff for EntryFrame class up here
-
-
class Controller(object):
-
def __init__(self):
-
self.myEntryFrame = EntryFrame(None, DIGITS)
-
-
myController = Controller()
Let me know if this is not what you're after or if you're having a problem with it.
@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
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] -
DIGITS=4
-
class EntryFrame(Frame):
-
"""A widget base class for entering a list of digits
-
-
Constructor: EntryFrame(parent, num) - parent is the parent widget
-
and num is the number of digits to be entered.
-
"""
-
-
----def __init__(self, parent, num):
-
--------Frame.__init__(self, parent)
-
--------self.entries = []
-
--------self.num = num
-
--------for i in range(num):
-
------------entry = Entry(self, width=1, font=GUESSFONT)
-
------------entry.pack(side=LEFT,padx=0)
-
------------entry.bind("<Key>", self.keyevent)
-
------------self.entries.append(entry)
-
--------self.entries[0].focus()
-
--------self.ok = Button(self, text="Guess", command=self.guess)
-
--------self.ok.pack(side=LEFT, padx=10)
-
--------self.cancel = Button(self, text="Clear", command=self.clear)
-
--------self.cancel.pack(side=LEFT, padx=10)
-
--------self.statustext = StringVar()
-
--------Label(self, textvariable=self.statustext,\
-
--------------anchor=W,width=30).pack(side=LEFT)
-
--------self.setStatus()
-
-
def keyevent(self, e):
-
"""The key event callback."""
-
-
index = self.entries.index(e.widget)
-
v = e.char
-
sym = e.keysym
-
if sym == 'Right': # rightarrow
-
index = (index+1)%self.num
-
self.entries[index].icursor(END)
-
self.entries[index].focus()
-
self.entries[index].select_range(0,END)
-
elif sym == 'Left': # leftarrow
-
index = (index-1)%self.num
-
self.entries[index].icursor(END)
-
self.entries[index].focus()
-
self.entries[index].select_range(0,END)
-
-
elif sym == 'BackSpace':
-
if index > 0 and self.entries[index].get() == '':
-
index -= 1
-
self.entries[index].focus()
-
self.entries[index].select_range(0,END)
-
self.after_idle(self.handleEntries, index)
-
elif sym == 'Return': # enter
-
self.guess()
-
elif sym == "Delete":
-
self.clear()
-
elif v.isdigit():
-
self.entries[index].delete(0,END)
-
if index < self.num-1:
-
index +=1
-
self.after_idle(self.handleEntries, index)
-
self.entries[index].focus()
-
self.entries[index].select_range(0,END)
-
elif v and (v.isalpha() or v in CHARS):
-
self.entries[index].delete(0,END)
-
self.handleEntries(index)
-
return "break"
-
-
def clear(self):
-
"""Clear all the entries."""
-
-
for i in range(self.num):
-
self.entries[i].delete(0,END)
-
self.handleEntries(i)
-
self.entries[0].focus()
-
self.setStatus()
-
-
def handleEntries(self, index):
-
"""The after_idle callback - highlight the text in the current
-
entry and process the entries
-
"""
-
-
self.entries[index].select_range(0,END)
-
self.processEntries()
-
-
def guess(self):
-
"""Called when a guess is entered - to be overridden
-
in the subclass."""
-
-
pass
-
-
def processEntries(self):
-
"""Called whenever a digit is added or deleted - to be overridden
-
in the subclass."""
-
-
pass
-
-
def setStatus(self):
-
"""Set the text in the status label - - to be overridden
-
in the subclass."""
-
-
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.
boxfish 469
Recognized Expert Contributor
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: - EntryFrame.setStatus(self)
@boxfish
is this what had. i think it's a little different to what you are saying i should do -
class View(EntryFrame):
-
def __init__(self, parent, num):
-
EntryFrame.__init__(self, parent, num)
-
self.pack(anchor=W)
-
def setStatus(self):
-
n=0
-
for i in self.entries:
-
if i in self.entries[n+1:]: #not too sure on this part
-
self.statustext.set('Status: Repeated Digits')
-
elif len(self.entries)==4:
-
self.statustext.set('Status: Ready')
-
else:
-
self.statustext.set('Status:')
-
n+=1
-
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?
boxfish 469
Recognized Expert Contributor
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: - entryDigits = []
-
for i in self.entries:
-
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(entr yDigits)) is ugly, but this loop should check for repeated digits: - repeatedDigits = False
-
for i in xrange(len(entryDigits)): # Loop through indices of entryDigits.
-
for j in entryDigits[i+1:]: # Loop through items in remaining part of entryDigits.
-
if j == entryDigits[i]: # Uh oh, repeated digit!
-
repeatedDigits = True # Set the flag.
-
break # Stop looking for repeated digits.
-
if repeatedDigits:
-
break # Make sure we stop looking for repeated digits.
I hope this is helpful. Let me know if it's not working.
so i overrode the process entries to call the setStatus function, and everything seems to be working. however - entryDigits = []
-
for i in self.entries:
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Richard Lewis |
last post by:
Hi there,
I've just started my first project with Tkinter.
I've already coded all the data handling classes and have a nice
interface to work with (it happens to be a wrapper around the DOM of a
large(ish) XML document but thats probably not important ;-)
I'm new to Tkinter so I've started in a fairly random place: the main
menu. I...
|
by: William Gill |
last post by:
A short while ago someone posted that(unlike the examples) you should
use Tk as the base for your main window in tkinter apps, not Frame. Thus :
class MyMain(Frame):
def __init__(self, master):
self.root = master
self.master=master
self.createWidgets()
def createWidgets():
...
|
by: Shaun Pudwell |
last post by:
Hi, I'm new to c# and come from a C / C++ background.
My MainWindow class has a function called DrawChart.
I open an MDI Child Window and want it to make a call from
its class MDIChild to DrawChart which is the MainWindow
class. Also, I want the chart counter to be incremented
on each call to DrawChart, without being set back to its...
|
by: Amanda |
last post by:
Let me see if I can explain this correctly.
In VB.NET, I want to call classes based on a recordset I get back from
a SQL Server database.
Basically, I have a value sending in a table called
cls.CallFunction(strParameter).
In VB.NET cls.CallFunction is a class and a function in my code.
|
by: Michael Yanowitz |
last post by:
Hello:
Below I have included a stripped down version of the GUI I am working on.
It contains 2 dialog boxes - one main and one settings. It has the following
problems, probably all related, that I am hoping someone knows what I am
doing wrong:
1) Pressing the Settings.. Button multiple times, brings up many instances
of the Settings...
| |
by: sumanthsclsdc |
last post by:
Hello friends,
I have a problem, I implemented a class which uses tkinter and displays the window as required, the class will create a window with listbox and inserts some items into it, I created the instance of this class and executed, I require the selected item to be sent back to the calling function, and the window should not be...
|
by: sandip desale |
last post by:
Dear All,
We have a Tcl/Tk application written using Python 2.2. Using this application we want to call some customizable Java APIs. I tried porting Tcl/Tk application to Jython but not able to do the same as TKinter library is not available with JYthon.
Can you please help me in porting Tkinter application to Jython? Also kindly let me...
|
by: wisaunders |
last post by:
I have several different classes that contain the same four
properties. I tell my application property of which class to call in
the configuration file.
Here are the values from my config file
<class>AAAA</class>
<property>1111</property>
|
by: Bellum |
last post by:
I'm rather new to both Tkinter and Classes, so whatever I'm doing wrong probably seems really stupid. Just bare with me, here. The reference to the buttons seems to disappear after the __init__ method, so when I try to call them and change them in another method, I get an attribute error.
def __init__(self):
self.window = Tk()
...
|
by: Kevin Walzer |
last post by:
I'm trying to create a custom Tkinter widget class, and I'm having some
difficulty getting it set up properly.
The class is called MacToolbar, saved in its own module MacToolbar.py,
and imported with this statement:
import MacToolbar
Here is the relevant portion of the class:
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |