[Tutor] How to use g_timeout_add () function?
Hello all, i have a question: when i check gtk_time_out in the gtk+2 reference, it said " |gtk_timeout_add|has been deprecated since version 2.4 and should not be used in newly-written code. Use |g_timeout_add()|instead." but i don't know how tu use the g_timout_add() function: my_id = g_timeout_add(500, myfunction()) or: my_id = gtk.g_timeout_add(500, myfunction()) everytime i run the program, it prompted me a message like modules do not have g_timeout_add() attribute. so i still have to use gtk_timeout_add anybody help me? Lion Chen ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] why gtk.Entry does not give me right answers?
Hi, All, i am trying write a calculator, now normal calculation works ok. but if i enter long numbers, for example 33 + 7, it gives me "12". there are 18 "3"s, if the length is more than 17, the calculation will be wrong on Windows and Ubuntu. the following is some of my codes: def pre_calc(self, op): #if entry is empty, any operators are invalid except "-" if not self.entry.get_text() and op != "-": return if not self.entry.get_text() and op == "-": self.entry.set_text(op) self.cleared = 1 self.negative_sign = True#indicates there's already a "-" return #retrieve the 1st num and operator if not self.op and self.entry.get_text(): if op != "=": self.op = op #print "text typ:", type(self.entry.get_text()) self.first_num = self.entry.get_text() self.cleared = 0 # ready to input 2nd num #print "op:", self.op, "-", "1st num:", self.first_num return else: self.first_num = None self.second_num = None self.op = None self.cleared = 0 self.negative_sign = False return #retrieve the second num and begin calculation elif self.op and self.first_num and self.entry.get_text() != "-": if op == "=": self.second_num = self.entry.get_text() self.calc() #reset the following varibles, awaiting next round of inputs self.first_num = None self.second_num = None self.op = None self.cleared = 0 self.negative_sign = False return elif op == "-": if self.cleared == 1: #if user has input 2nd num already self.second_num = self.entry.get_text() self.calc() self.first_num = self.entry.get_text() self.op = op self.cleared = 0 self.negative_sign = False return else: self.entry.set_text(op) self.cleared = 1 self.negative_sign = True#indicates there's already a "-" return else: self.op = op if self.cleared == 1: self.second_num = self.entry.get_text() self.calc() self.first_num = self.entry.get_text() self.cleared = 0 self.negative_sign = False return else: return def calc(self, unary_op = None): if not unary_op: if (not self.first_num) or (not self.second_num) or \ (not self.op): return result = None calc_methods = {"+": self.add, "-": self.subtract, "*": self.multiply, "/": self.divide, "%": self.mod, "pwr": self.power, "|": self.b_or, "&": self.b_and, "^": self.b_exc, "<<": self.b_lshift, ">>": self.b_rshift } unary_methods ={"~": self.b_invert, "1/x": self.a_fraction, "sqrt": self.sqrt } if not unary_op: for op, method in calc_methods.items(): if self.op == op: result = calc_methods[op]() else: for op, method in unary_methods.items(): if unary_op == op: result = unary_methods[op]() self.entry.set_text(result) def add(self): result = None #if hex system #print type(self.first_num) if self.hdob["h"].get_active(): self.first_num = long(self.first_num, 16) self.second_num = long(self.second_num, 16) r = self.first_num + self.second_num r = self.convert.d2h(r) return r #if decimal system elif self.hdob["d"].get_active(): self.first_num = float(self.first_num) self.second_num = float(self.second_num) result = self.first_num + self.second_num if result - long(result) == 0: #print str(long(result)) return str(long(result)) else: return str(result) #if octal system elif self.hdob["o"].get_active(): self.first_num = long(self.first_num, 8) self.second_num = long(self.second_num, 8) result = self.first_num + self.second_num result = self.convert.d2o(result) return result else: self.first_num = long(self.first_num, 2) self.second_num = long(self.second_num, 2) result = bin(self.first_num + self.second_num) result = self.convert.d2b(result) return result but if i run the following codes on command line, it works ok: >>>a = 33L >>>b= str(a) >>>b '33' why? i don't understand anybody can help me? thanks. Lion ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why gtk.Entry does not give me right answers?
于 2012年04月23日 23:08, Steven D'Aprano 写道: Lion Chen wrote: Hi, All, i am trying write a calculator, now normal calculation works ok. but if i enter long numbers, for example 33 + 7, it gives me "12". there are 18 "3"s, if the length is more than 17, the calculation will be wrong on Windows and Ubuntu. You are losing precision by converting into a float. Watch: n = 33 n + 7 40 int(float(n)) 12 int(float(n)+7) 12 Your integer value 333...333 requires 59 bits to store the entire number, but Python floats are C doubles, which only have 52 bits available for the numeric digits. (The other 12 bits encode the sign and exponent.) http://www.johndcook.com/blog/2009/04/06/anatomy-of-a-floating-point-number/ You cannot store ...333 exactly as a float. The two closest floats are: 12.0 76.0 so all numbers between them will be rounded up or down. Some possible solutions: * Don't try to mix infinite-precision integer arithmetic with finite-precision float arithmetic. * Don't convert to float unless you really need to, e.g. if the string contains a decimal point. * Use the decimal module instead of floats. You still have finite precision, but you can choose how many *decimal* places to store instead of having a fixed number of *binary* places. (But decimal is much slower.) By the way, the code that you sent is unreadable. Please make sure that you send plain text email, and that indentation is not lost. Without indentation, it is impossible to understand your code. many thanks, Steve, i see, and i have solved it :) . and the codes in my thunderbird shows ok, so i thought it would be ok in yours, sorry for that Lion ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] gtk.TreeView displays all right on Windows, but can't see the values on Ubuntu
Hi, All, first thanks the people who gave helps to me, :) now i encountered another question... my first program LionCalculator, works ok on Windows, but there is a problem on Ubuntu. LionCalculator has a function that can store the previous calculation results to the gtk.ListStore((gobject.TYPE_STRING, gobject.TYPE_BOOLEAN, gobject.TYPE_STRING)) for i in xrange(12): self.results_store.append(["F%d" % (i+1), None, None]) when pressed F1--F12 or click the gtk.CellRendererRadio, the gtk.Entry will get the corresbonding values stored in ListStore. when i run the program on Windows, no problem, i can see everything that should display on the screen. but when i run it on Ubuntu, i can't see the "F1" -- "F12" in the ListStore, can't see the calculation results that should also display on ListStore, only the gtk.CellRendererRadio showed. i don't understand... and when i add "print ...get_store()[0][0]" and run it again, it print "F1"! strange... and when i press F1 or F2..., the result will copy to the entry, although it does not display in the TreeView, strange... the following are codes: #in class ResultsView.. def make_view(self): # make ListStore for storing results self.results_store = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_BOOLEAN, gobject.TYPE_STRING) #to store 12 calculation results, the results_store[0][0] is "F1--F12" for i in xrange(12): self.results_store.append(["F%d" % (i+1), None, None]) self.results_view = gtk.TreeView(self.results_store) #f_renderer is for "F1" -- "F12" f_renderer = gtk.CellRendererText() f_renderer.set_property( 'editable', False ) f_renderer.set_property("size", 5) f_renderer.set_property("cell-background", "cyan") #when clicked bt_renderer, it will copy the corresbonding values to gtk.entry bt_renderer = gtk.CellRendererToggle() bt_renderer.set_property('activatable', True) bt_renderer.set_property("radio", True) bt_renderer.set_property("cell-background", "grey") bt_renderer.connect("toggled", self.ready_cp, self.results_store) #txt_renderer is for storing calculation results txt_renderer = gtk.CellRendererText() txt_renderer.set_property( 'editable', False ) txt_renderer.set_property("size", 5) txt_renderer.set_property("cell-background", "green") #i guess the problem is in the following, but i don't know where it exactly is, ok in Windows, can't show in Ubuntu... bt_column = gtk.TreeViewColumn("F1--F12") bt_column.pack_start(f_renderer, True) bt_column.pack_start(bt_renderer, False) bt_column.set_attributes(f_renderer, text=0) #set active to be clickable. and the bt_columen is #corresbonding to results_store columne 1 bt_column.add_attribute(bt_renderer, "active", 1) #and txt_column is corresbonding to the store column 2 txt_column = gtk.TreeViewColumn("Calculation Results ", txt_renderer, text=2) self.results_view.append_column(bt_column) self.results_view.append_column(txt_column) self.results_view.show() return self.results_view #in class LionCalc def __init__(self): .. self.results_view = ResultsView() right_vbox.pack_start(self.results_view.make_view(), True, True, 0) win.show_all() could anybody give me help? thanks. -- Lion Chen ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] why list is not thread-safe, but deque and queue are thread-safe?
Hi, All, i can not understand why list is not thread-safe, but deque and queue are thread-safe? that's all. thanks. Lion Chen ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why list is not thread-safe, but deque and queue are thread-safe?
i have read it, as the "Answer" said, seems like deque is not thread-safe either, because deque has the operator +=, and others like that. i got some material about atomoperation from google: An operation during which a processor <http://www.webopedia.com/TERM/M/microprocessor.html> can simultaneously read <http://www.webopedia.com/TERM/R/read.html> a location and write <http://www.webopedia.com/TERM/W/write.html> it in the same bus <http://www.webopedia.com/TERM/B/bus.html> operation. This prevents any other processor or I/O <http://www.webopedia.com/TERM/I/I_O.html> device <http://www.webopedia.com/TERM/D/device.html> from writing or reading memory until the operation is complete./ Atomic/ implies indivisibility and irreducibility, so an atomic operation must be performed entirely or not performed at all. so deque is not thread-safe either. right? http://stackoverflow.com/questions/6319207/are-lists-thread-safe On Wed, May 2, 2012 at 7:58 PM, Lion Chen <mailto:chnlio...@gmail.com>> wrote: Hi, All, i can not understand why list is not thread-safe, but deque and queue are thread-safe? that's all. thanks. Lion Chen ___ Tutor maillist - Tutor@python.org <mailto:Tutor@python.org> To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- http://spawgi.wordpress.com We can do it and do it better. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] why i < j is True, j < k is False
Hi, All, here are the codes: class a: pass i = a () j = a () k = a () i < j returns True j < k returns False why? Lion Chen ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why i < j is True, j < k is False
On Fri, May 4, 2012 at 9:29 AM, Lion Chen wrote: Hi, All, here are the codes: class a: pass i = a () j = a () k = a () i< j returns True j< k returns False why? Lion Chen ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Did you look at what I, j, and k are? They are names of objects. When I ran your code my inequalities were different. What you are seeing is likely some comparison of the location of the objects over which you have no control i = a() j = a() k = a() i< j False j< k True i <__main__.a instance at 0xb76d0a2c> j <__main__.a instance at 0xb76d096c> k <__main__.a instance at 0xb76d09ec> but why have i got the same results every time i run the codes? even when i restart the python interpreter, seems like i, j, k have the fixed values. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why i < j is True, j < k is False
于 2012年05月04日 22:12, Dave Angel 写道: On 05/04/2012 09:29 AM, Lion Chen wrote: Hi, All, here are the codes: class a: pass i = a () j = a () k = a () i< j returns True j< k returns False why? Lion Chen ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor i, j and k are three distinct instances of the class a (which should be capitalized for clarity). Since you don't supply any comparison operator special methods in your class, Python 2 simply compares their id values. So the ordering is entirely undefined, and each of us might get different results. I, for example, got true and true. In Python 3, you'd get the following error, which is an improvement, imho: TypeError: unorderable types: A()< A() i see, thanks :) Lion Chen ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor