404 Posted Topics
Re: Yes, yes, and yes. Welcome to the freedom of Python. :) First of all, as is typical for Python, there is a module called time (and another one, datetime) that does exactly this stuff automagically. Look them up in the Python docs. But if you wanted to do it by … | |
Re: Well, there's two problems. First, you have to define the list lbl_addy before you start assigning to it. Second, the name of your newly created Label in line 6 is lbl_addy[k]. So in line 7, you need to pack it like this: lbl_addy[k].pack() Hope that helps, Jeff | |
Re: Hi. A couple of suggestions. (1) (minor) mydict2 should be generated dynamically from mydict1 so that if you ever decide to change mydict1, it will automagically change along with it. Here's how. Replace the entirety of mydict2 with this line: mydict2 = dict(zip(mydict1.values, mydict1.keys)) This will make a bunch of … | |
Re: Hm. The IDLE shell's purpose is for instant code testing. If you want to write a program, then <CTRL>-N gives you a new window to write code to. Is that what you needed? Jeff | |
Re: Exactly so. Install PIL from the link above, and then [code="Python"] from PIL import Image, ImageTk my_im = Image.open("myjpg.jpg") real_im = ImageTk.PhotoImage(my_im) [/code] and now you can use real_im in your Tkinter code. WARNING!!! There is a bug in Tkinter such that if you do not keep a hard reference … | |
Re: I watched it for a while and didn't see any rectangles being deleted. But you're right; the algorithm doesn't make a rectangle shape. So what's the issue, exactly? Jeff | |
Re: Yeah, if you think XML is a pain, wait until you try to create the OLE file required for the .doc format. :lol: 300+ pages of documentation, and I gave up in disgust. Jeff | |
Re: It looks like you are simply blit-ing from the source to the destination at the same size. You'll need to rescale the image first. When you say you are using Canvas and Image, are you referring to Image from the PIL library? Jeff | |
Re: You would have to find a way to emulate somefunc, I think. Jeff | |
Re: I'm surprised that it hangs rather than giving an error message. Your code references text1 before text1 is ever defined. I recommend this procedure for debugging: First, write a small program that simply creates a text widget and writes something to it. Then, transfer what you've learned back to this … | |
Re: Two different outputs on 4 are strange, yes. What two outputs are you getting? Jeff | |
Re: You could check out the os and os.path modules. Jeff | |
Re: Sorry, didn't see this thread before. I'm afraid it doesn't work ... :( [code="unittesting"] Enter the real number you would like to convert to binary: 3.1 11.0001100110011 Would you like to continue? y/n : y Enter the real number you would like to convert to binary: 2.5 10. Would you … | |
Re: >>> unicode("India is") u'India is' Dunno. What version of Python are you running? Jeff | |
Re: Hmm... I'm getting a different set of messages: [code="Error"] Warning (from warnings module): File "C:/Python25/menuapp.py", line 8 from wxPython.wx import * #import the GUI DeprecationWarning: The wxPython compatibility package is no longer automatically generated or actively maintained. Please switch to the wx package as soon as possible. Traceback (most recent … | |
Re: Well, walk through your code: [code="Python"] class Superlist(list): def __init__(self, list): self.a = list[1][0] self.b = list[2][1] somelist = [[1,2,3],[4,5,6],[7,8,9]] somelist = Superlist(somelist) [/code] At line 8, you create a new Superlist object. This creates a new list [], and then calls __init__. At init, you create two new properties … | |
Re: One way to eliminate the "if" clause is like this: chr(ord(c) - 65 + num) % 26 + 65 It subtracts the ord('A') to get the letter number (from 0...25), rotates the letter using %, and then adds the ord('A') back on. Showcasing three favorite Pythonisms: anonymous functions, list generators, … | |
Re: So it turns out that Timer objects don't receive normal callback functions, but event handler objects. If you don't feel like making one, then you can subclass the Timer and make your own Notify() method for it. That's what I did here: [code="Python"] import wx class MyTimer(wx.Timer): def Notify(self): m.frame.MB.Close(True) … | |
Re: To my inexpert eye, it looks like you have to assign each item a different id number. Then, wx.EVT_LIST_ITEM_SELECTED will tell you which item is selected. That's a non-expert opinion, BTW. Jeff | |
Re: Here's a start: [code="Python"] digits = {0:"zero, 1:"one", 2:"two", etc.} print digits[2] [/code] HTH, Jeff | |
I'm having an opaque moment. +5 brownie points to anyone who can enlighten. The plan is to write a unit-testing module for the wxCanvas widget (good progress there :) ) So I wanted a row of different objects (similar to MS Paint) on buttons; the behavior of the different buttons … | |
Re: Here's a very basic example: [code="Python"] import threading import time def handler(): for i in range(5): time.sleep(5) print "Hi!" t = threading.Thread(target=handler) t.start() for i in range(20): print "hello!" time.sleep(1) [/code] handler() will chime in every 5 seconds. Jeff | |
Re: Well, a couple of things come to mind. This is a commendable start. First of all, how would you solve this problem on paper? Suppose I gave you a string "dog" and said "find all of the letters in 'dog' in 'abcdefghijklmnopqrstuvwxyz' and tell me how many compares it takes … | |
Re: For a 2D array, a list-of-lists can work: [code="Python"] >>> a = [[1,2,3],[4,5,6],[7,8,9]] >>> a[1][2] 6 >>> [/code] Jeff | |
Re: you have to import the module that contains that function first: [code="Python"] >>> import random >>> random.randint(1,3) 1 [/code] Jeff | |
| |
Re: That's a good function. You did all of the sensible optimizations. But now, notice that your design requirement is to print the factors of your number ... so ... modify your function (same loop, different action at the if n % x == 0: branch) so that it generates a … | |
Re: Nice job! Here's a nitpicky critique -- take it as a compliment to your obvious talent at picking up a language in short order. Am I correct in thinking that your background is in C? (1) no '' is needed after ','. Python accepts a comma as a line continuation … | |
Re: Couldn't find anything. Is there a reason you wouldn't want to use Newton's Method to approximate a solution? Jeff | |
Re: Well, here're a couple of suggestions: [code="Python"] import wx class KeyEvent(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title) ## <<< self.panel = wx.Panel(self, -1,size=wx.Size(100,100)) ## <<< self.button = wx.Button(self, -1, label="click me") self.sizer = wx.BoxSizer(orient=wx.VERTICAL) ## basic sizer stuff self.sizer.Add(self.panel) self.sizer.Add(self.button) self.SetSizer(self.sizer) ## I always forget to do … | |
Re: I've had intermittent problems over the last week. Reloading pages usually solves the problem. Time for a new server at wxpython.org, I think. Jeff | |
Hi all, I want to write a function that returns a bounding box for a wxPython polygon as in wx.DC.DrawPolygon. Essentially, the polygon is defined in terms of a list of points [(x0,y0),(x1,y1),...,(xn,yn)]. The bounding rectangle is clearly (xmin, ymin, xmax, ymax), or in terms of the wx.Rect structure, wx.Rect(xmin, … | |
Re: The good folk over in the C++ forum will be happy to, I'm sure. wxWidgets is written in C++, and wxPython sits on top of it. Are you trying to figure out something in wxPython? Jeff | |
Re: The first goal is met with simple replace: [code="Python"] >>> mystring="""1243#74656453634#6356#56456456#565#1212121#7838483 #457#090#720347###24273#18712#478230478#5711758125671#47464 #74647#46474647#19192938#828##25835#2948284#6010203040#""" >>> mystring2 = mystring.replace("#","!") >>> print mystring2 1243!74656453634!6356!56456456!565!1212121!7838483 !457!090!720347!!!24273!18712!478230478!5711758125671!47464 !74647!46474647!19192938!828!!25835!2948284!6010203040! [/code] The second goal is unclear. Do you want to replace the first three ! with @, or what? Jeff | |
Re: Yes, you want to use xrange() instead of range(). xrange() creates an 'iterator' that loops through one at a time, while range() creates the whole list in place. [code="Python"] from math import sqrt f=lambda n: sum([int(i)*int(i) for i in str(n)]) L=[] for n in range(0,10**8): if sqrt(f(n))%1==0:# == float(int(sqrt(f(n)))): L.append(n) … | |
Hi all, This is the beginning of a hopefully useful project: to create a Canvas widget for wxPython that will mimic or improve on the Tkinter Canvas widget. Those of you who have coded with both will probably know what I'm talking about. With Tkinter, you can plop items like … | |
Re: I'm confused about the nature of the problem. Are you asking "When on June 1, 2008 did the sun enter the window @30 and leave the window @45?" 'Cause with the data you have, the sun never leaves that window... Jeff | |
Re: This wants to be OOP code, I think. The problem you're having is extracting the date meaningfully from the records. The solution is to create a class for the log entries and let the class worry about how to do it. Here's a crude example: [code="Python"] # logreader import datetime … | |
Re: Hmm... where'd you get the file? Opening it in IDLE is not likely to work. :lol: Jeff | |
Re: Hi nioasumi, Fine, thanks. The best way to help is to see the offending code. Just post it here, surrounded by [ code ] tags. Jeff | |
Re: And don't forget to understand the underlying math. If you're incrementing each value by its own value, then what you get is twice that value: [code] a = [1,2,3,4] b = [2*x for x in a] [/code] OR if you are incrementing each value by its place in the list, … | |
Re: First, the newer Python constructions pretty much eliminate the need for "map()". Try this: [code] squaredigits = lambda x: sum([int(i)*int(i) for i in x]) [/code] Jeff | |
Re: Great. Now I have to learn "'nicode". | |
Re: No, but I could imagine doing so. What's the state of your code. Jeff | |
Re: Great game. I've used it with my math students once upon a time. Rumor has it that Bill Gates took a while to figure it out. :) Jeff | |
Re: 1. Usually, you use a counter like this: [code="python"] counter = 0 # initialize counter here for line in infile: if "NAME" in line: continue counter += 1 # increment counter here line = line.rstrip() fields = line.split(',') name, iso, capital, population, area = fields if int(population) >= p: print … | |
Re: That's a steep learning curve! One day of Python, and *POOF*, file structures. Jeff | |
Re: Your code is broken as it stands. What's the URLLister() class? I don't have it in my urllib. Jeff | |
Re: [quote]...where in hell is the tax equation...[/quote] Massachusetts. Seriously, this line: [code] output.setText("%0.1% tax) [/code] should read [code] output.setText("%0.1f" % tax) [/code] I think. Use print statements to figure out where the problem is exactly. Jeff |
The End.