[Tutor] glade gtk radiobutton group
I'm trying to create an incredibly simple application just to learn gui programming, but I can't see how to work with radio buttons. A sample from my glade looks like: Ch 1 True True False True 1 Ch 2 True True False True radiobutton1 ...and so on. 11 radiobuttons, all belonging to group "radiobutton1" I've tried many different things in my python file, things like radio = [r for r in cbc['radiobutton1'].get_group() if r.get_active()] (Saw that somewhere in my search) So, I have in my Class: dic = { "on_button_quit_clicked" : self.button_quit_clicked, "on_window_destroy" : self.button_quit_clicked, "on_MainWindow_destroy" : gtk.main_quit, "on_button_print_clicked" : self.button_print_clicked, "on_button_edit_clicked" : self.button_edit_clicked } And those all work fine so far. ie: def button_print_clicked(self, widget): print "Print clicked" And when I click the "Print" button, it prints to the terminal. As I said, very basic. Can someone tell me, show me, a very basic way of connecting to my radio buttons. If I could get the thing to just print "Radio button 3 is selected" I'd be ecstatic at this point. I did read through the tutorial, but my old mind isn't flexible enough to go from the hardcoded gtk examples to separate glade + python files. -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Regular expression grouping insert thingy
This is so trivial (or should be), but I can't figure it out. I'm trying to do what in vim is :s/\([0-9]\)x/\1*x/ That is, "find a number followed by an x and put a "*" in between the number and the x" So, if the string is "6443x - 3", I'll get back "6443*x - 3" I won't write down all the things I've tried, but suffice it to say, nothing has done it. I just found myself figuring out how to call sed and realized that this should be a one-liner in python too. Any ideas? I've read a lot of documentation, but I just can't figure it out. Thanks. -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regular expression grouping insert thingy
Oh. Crap, I knew it would be something simple, but honestly, I don't think that I would have gotten there. Thank you so much. Seriously saved me more grey hair. Matthew Wood wrote: re.sub(r'(\d+)x', r'\1*x', input_text) -- I enjoy haiku but sometimes they don't make sense; refrigerator? On Tue, Jun 8, 2010 at 10:11 PM, Lang Hurst <mailto:l...@tharin.com>> wrote: This is so trivial (or should be), but I can't figure it out. I'm trying to do what in vim is :s/\([0-9]\)x/\1*x/ That is, "find a number followed by an x and put a "*" in between the number and the x" So, if the string is "6443x - 3", I'll get back "6443*x - 3" I won't write down all the things I've tried, but suffice it to say, nothing has done it. I just found myself figuring out how to call sed and realized that this should be a one-liner in python too. Any ideas? I've read a lot of documentation, but I just can't figure it out. Thanks. -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org <mailto:Tutor@python.org> To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Structuring a class
I'm trying to figure out how to deal with data which will look something like: Student:Bob Hurley ID: 123456 Period:4 Grad_class: 2012 Credits: Algebra C (20P) Chapter 1 Date: September 14, 2010 Grade: 87 Credits:1.5 Notes: Probably cheated on final. Really watch on next quiz/test. Chapter 2 Date: October 31, 2010 . . **and so on** . Consumer Math (24G) Module 2 . . **more information like above** . So, I just figured that I would have a couple of nested dictionaries. bob = Student('123456','Bob Hurley') bob.period = 4 bob.grad_class = 2010 bob['credits']['Algebra C (20P)']={'Chapter 1':{'Date':'September 12, 2010', 'Grade':'87', **and so on**}} This works, for the most part, from the command line. So I decided to set up a class and see if I could work it from that standpoint (I'm doing this to scratch an itch, and try to learn). My preliminary class looks like: class Student: def __init__(self, ident=' ', name=' ', period=' ', grad_class=' ', subject=' ', notes=' ', credit=' '): self.name = name self.ident = ident self.period = period self.notes = notes self.grad_class = grad_class # self.credit = {{}}<--- BAD self.credit = {} I'm sure that someone will enlighten me as to where my poor coding skills are especially weak. It's the last line there (self.credit...) which I can't figure out. The credits may be in any of about 30 different subjects (teaching at a continuation high school makes for interesting times). If I don't set it to anything, ie, like it is, I get an error Stud instance has no attribute '__getitem__' If I try to leave it available for adding to somewhat dynamically, I get a 'wtf' from python. Sorry for the novel, I'm just wondering if someone would set me straight. Should I even use a class? If so, how do I set up a class item which let's me add dictionaries to it? Thanks. -Lang -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Structuring a class
bob gailer wrote: Often a case like this is better handled using a relational database. Python happens to come with the sqlite3 module which makes database work quite easy. You should define a class for Credit, which will hold the credit attributes, just like you did for Student. Then assign instances of Credit to entries in self.credit. Last time I did anything with python, it was years ago on a web application and I ended up using gadfly as a database. I did not know that sqlite3 came as a module for python. That just made it much, much easier. I've done much more work with databases. Thank you. I figured that I was probably missing something obvious. -Lang -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] sqlite3 select extra characters
Is there a way to just return the values when using sqlite3? If I: names = c.execute('select names from students') for name in names: print names I get the list I want, but they look like (u'Cleese, John') (u'Jones, Terry') (u'Gillaim, Terry') is there a way to just return Cleese, John Jones, Terry Gillaim, Terry ? It seems a shame to have to run the answers through a stip process. -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sqlite3 select extra characters
Thanks. Alan Gauld wrote: "Lang Hurst" wrote Is there a way to just return the values when using sqlite3? I don't think so, select returns a tuple of values, even if there is only one value. I get the list I want, but they look like (u'Cleese, John') is there a way to just return Cleese, John It seems a shame to have to run the answers through a stip process. It's hardly a major effort to index the item? print name[0] instead of print name HTH, -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sqlite3 select extra characters
Steven D'Aprano wrote: On Fri, 18 Jun 2010 03:54:05 pm Lang Hurst wrote: Is there a way to just return the values when using sqlite3? If I: names = c.execute('select names from students') for name in names: print names What is c? A Cursor or a Connection object? It probably doesn't make any real difference, but it could, and so you should say. I'm doing my best. c was a cursor. Perhaps not the best naming convention. [snip] I doubt it. Did you mean this? (u'Cleese, John',) Note the comma at the end. A subtle difference, but a HUGE one. Punctuation is important: it's what makes the difference between: Yup, you're right. In Python, the comma makes it a tuple, rather than a meaningless pair of parentheses. is there a way to just return Cleese, John Jones, Terry Gillaim, Terry No, because you're not *returning* anything, you're *printing*, and printing a tuple prints the opening and closing brackets plus the repr() of each internal object. I guess I should have been more clear. By return, I suppose I meant assign. I'm trying to pull the names from rows of data and append each name to a ListStore, for entry completion. I was using "print" just to show what I was hoping to get into the ListStore, and at that point, I was only getting the tuple, not the object. This is fundamental stuff: objects are not their printable representation, any more than you are a photo of you. What is printed is not the same as the object itself. Every object has its own printable representation. In some cases it looks exactly the same as the way you enter the object as a literal: [snip] In this case, the object returned by execute is a list of tuples. If there is only a single object in each tuple, it is still a tuple, and it still prints as a tuple. If you want to print something other than a tuple, don't print the tuple: names = c.execute('select names from students') for t in names: name = t[0] # Extract the first item from each tuple. print name Of course you can combine the two lines inside the loop: print t[0] OK. It seems a shame to have to run the answers through a stip process. It would be more of a shame if execute sometimes returned a list of tuples, and sometimes a list of other objects, with no easy way before hand of knowing what it would return. Actually, I know you were being facetious, but with my skills so poor, often that seems like what is happening. I'm killing myself right now trying to troubleshoot glade, python and entrycompletion. I know it will soon be as clear as a tuple vs an object; I just hope that transition happens soon. Thanks again for walking me through the terminology, it is appreciated. -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python glade
OK, I created a UI in glade which has the following: True True ● 25 basically, a text box. I'm trying to set it up to automatically complete names as I type. My py file has the following: def __init__(self): self.builder = gtk.Builder() self.builder.add_from_file('gradebook.glade') self.window = self.builder.get_object('winapp') self.builder.connect_signals(self) # self.student_change = gtk.Entry() completion = gtk.EntryCompletion() self.names = gtk.ListStore(str) query = "SELECT * from students" db = sqlite3.connect('gradebook.db') cursor = db.cursor() cursor.execute(query) students = cursor.fetchall() for student in students: self.names.append([student[1]]) print student[1] cursor.close() completion.set_model(self.names) self.student_change.set_completion(completion) completion.set_text_column(0) When I try to run this, I get AttributeError: 'appGUI' object has no attribute 'student_change' But if I uncomment the self.student_change line from up above, it runs but doesn't do completion. I modeled this after http://www.koders.com/python/fid755022E2A82A54C79A7CF86C00438E6F825676C3.aspx?s=gtk#L4 I'm pretty sure the problem is somewhere in the gtk.Builder part of what I'm doing, but I can't for the life of me figure this out. -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python glade
Found the problem. If you want to do this, you have to access the gtkEntry like this self.builder.get_object('student_change').set_completion(completion) instead of self.student_change.set_completion(completion) Lang Hurst wrote: OK, I created a UI in glade which has the following: True True name="invisible_char">● 25 basically, a text box. I'm trying to set it up to automatically complete names as I type. My py file has the following: def __init__(self): self.builder = gtk.Builder() self.builder.add_from_file('gradebook.glade') self.window = self.builder.get_object('winapp') self.builder.connect_signals(self) # self.student_change = gtk.Entry() completion = gtk.EntryCompletion() self.names = gtk.ListStore(str) query = "SELECT * from students" db = sqlite3.connect('gradebook.db') cursor = db.cursor() cursor.execute(query) students = cursor.fetchall() for student in students: self.names.append([student[1]]) print student[1] cursor.close() completion.set_model(self.names) self.student_change.set_completion(completion) completion.set_text_column(0) When I try to run this, I get AttributeError: 'appGUI' object has no attribute 'student_change' But if I uncomment the self.student_change line from up above, it runs but doesn't do completion. I modeled this after http://www.koders.com/python/fid755022E2A82A54C79A7CF86C00438E6F825676C3.aspx?s=gtk#L4 I'm pretty sure the problem is somewhere in the gtk.Builder part of what I'm doing, but I can't for the life of me figure this out. -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to add extra Vbox fields dynamically
I hope that I'm asking this in the right place. I don't have too much trouble hacking together command line stuff, but the GUI part is a struggle for me. I created a UI in glade. It has a couple of Vboxes for information. The final box is filled with a TextView. In my program, I'm connecting to a database and pulling out a series of records. As it stands, I can pull out all the records and view them in the TextView, but I would like to be able to have each result be a separate TextView (which I then have to figure out how to make clickable...) Right now, this part looks like: query = 'SELECT subject, chapter_module, credits, final_test_score, notes FROM credits WHERE id=' + student[0][6] cursor.execute(query) credits = cursor.fetchall() temp = '' for credit in credits: sub_buf = 15 - len(credit[0]) chap_buf = 15 - len(credit[1]) cred_buf = 5 - len(credit[2]) score_buf = 5 - len(credit[1]) temp = temp + credit[0] + " " * sub_buf + credit[1] + " " * chap_buf + "Credits: " + credit[2] + " " * chap_buf + "Score: " + credit[3] + "\n\nNOTES: " + credit[4] + "\n" + " " * 5 + "-" * 50 + "\n\n" # I would like to loop something here # to have multiple text areas added buff = self.builder.get_object('textview1').get_buffer() buff.set_text(temp) This works fine. It pulls the records out of the database, and then cats the results together and throws it into my TextView. I'm happy with the results so far, but I would like to be able to click on each record if I see something that needs to be modified. As always, any help is appreciated. Also, can anyone recommend a good book for gtk + glade + python? I went out and bought Learning Python, but book at B&N that were remotely GUI related seems very outdated or just tkinter related, and just about all the gtk+python examples and tutorials don't use glade. Thanks again. -Lang -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to add extra Vbox fields dynamically
OK, I just did the ugliest hack, from someone who only seems to do ugly hacks. I set up a bunch of textview areas and defaulted them to 'not visible'. Then as I loop through my query results, I make them visible one at a time. Well, it works perfect, but it just doesn't seem right for some reason. Lang Hurst wrote: I hope that I'm asking this in the right place. I don't have too much trouble hacking together command line stuff, but the GUI part is a struggle for me. I created a UI in glade. It has a couple of Vboxes for information. The final box is filled with a TextView. In my program, I'm connecting to a database and pulling out a series of records. As it stands, I can pull out all the records and view them in the TextView, but I would like to be able to have each result be a separate TextView (which I then have to figure out how to make clickable...) Right now, this part looks like: query = 'SELECT subject, chapter_module, credits, final_test_score, notes FROM credits WHERE id=' + student[0][6] cursor.execute(query) credits = cursor.fetchall() temp = '' for credit in credits: sub_buf = 15 - len(credit[0]) chap_buf = 15 - len(credit[1]) cred_buf = 5 - len(credit[2]) score_buf = 5 - len(credit[1]) temp = temp + credit[0] + " " * sub_buf + credit[1] + " " * chap_buf + "Credits: " + credit[2] + " " * chap_buf + "Score: " + credit[3] + "\n\nNOTES: " + credit[4] + "\n" + " " * 5 + "-" * 50 + "\n\n" # I would like to loop something here # to have multiple text areas added buff = self.builder.get_object('textview1').get_buffer() buff.set_text(temp) This works fine. It pulls the records out of the database, and then cats the results together and throws it into my TextView. I'm happy with the results so far, but I would like to be able to click on each record if I see something that needs to be modified. As always, any help is appreciated. Also, can anyone recommend a good book for gtk + glade + python? I went out and bought Learning Python, but book at B&N that were remotely GUI related seems very outdated or just tkinter related, and just about all the gtk+python examples and tutorials don't use glade. Thanks again. -Lang -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to add extra Vbox fields dynamically
OK, figured that was probably bad etiquette, but there doesn't seem to be close to the same traffic. Mea culpa. I won't do it again. I think most of my issues have to do with the gtk part, so I'll post there for the most part. Thanks. Timo wrote: On 20-06-10 04:04, Lang Hurst wrote: OK, I just did the ugliest hack, from someone who only seems to do ugly hacks. I set up a bunch of textview areas and defaulted them to 'not visible'. Then as I loop through my query results, I make them visible one at a time. Well, it works perfect, but it just doesn't seem right for some reason. You also posted this on the PyGTK mailing list, which is the correct place for these problems. Please post your PyGTK questions only there, do not double post. I'll answer you there. Cheers, Timo Lang Hurst wrote: I hope that I'm asking this in the right place. I don't have too much trouble hacking together command line stuff, but the GUI part is a struggle for me. I created a UI in glade. It has a couple of Vboxes for information. The final box is filled with a TextView. In my program, I'm connecting to a database and pulling out a series of records. As it stands, I can pull out all the records and view them in the TextView, but I would like to be able to have each result be a separate TextView (which I then have to figure out how to make clickable...) Right now, this part looks like: query = 'SELECT subject, chapter_module, credits, final_test_score, notes FROM credits WHERE id=' + student[0][6] cursor.execute(query) credits = cursor.fetchall() temp = '' for credit in credits: sub_buf = 15 - len(credit[0]) chap_buf = 15 - len(credit[1]) cred_buf = 5 - len(credit[2]) score_buf = 5 - len(credit[1]) temp = temp + credit[0] + " " * sub_buf + credit[1] + " " * chap_buf + "Credits: " + credit[2] + " " * chap_buf + "Score: " + credit[3] + "\n\nNOTES: " + credit[4] + "\n" + " " * 5 + "-" * 50 + "\n\n" # I would like to loop something here # to have multiple text areas added buff = self.builder.get_object('textview1').get_buffer() buff.set_text(temp) This works fine. It pulls the records out of the database, and then cats the results together and throws it into my TextView. I'm happy with the results so far, but I would like to be able to click on each record if I see something that needs to be modified. As always, any help is appreciated. Also, can anyone recommend a good book for gtk + glade + python? I went out and bought Learning Python, but book at B&N that were remotely GUI related seems very outdated or just tkinter related, and just about all the gtk+python examples and tutorials don't use glade. Thanks again. -Lang ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading Excel Files
Carlos Daniel Ruvalcaba Valenzuela wrote: Hello list, I was wondering if anyone has worked with excel 2007 files (importing data from), I have done so for old format (xls) via a number of modules like xlrd and the old pyexcelerator, however none of those packages currently support new 2007 format, although xlrd may have support for it later. Couldn't you just save it in the older format? Has anyone had to deal with something like this recently?, I'm thinking as last resort just work with the underlying XML files of the format, but it would be nice to have an already working module. The files I work with are pretty basic, so I just export them as cvs and work from there. Probably not what you want. Regards, Carlos Ruvalcaba ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: HELP!
On 06/28/2010 01:36 PM, Chris wrote: My friend jacob had a question. I can't really answer it, but you guys can. Send you replies to him not me. Oh, and jacob, I'm forwarding you message to the Python Mailing list. Chris, I tried the py2exe thing again, but things are getting confusing... It tells me to enter this line to run a setup that will make it a .exe. "C: \Tutorial>python setup.py install" Now, the REALLY BIG problem with this line is that the, I guess you could say action, of install always comes back with an invalid token error. This is straight from the website. Now, that was in different programs and on the command line. If you write it in the setup.py, it comes back with a syntax error after the install... Please help For what it's worth, py2exe never really worked for me the one time I was trying to bundle up a program. I ended up using pyinstaller ( http://www.pyinstaller.org/ ) and it worked pretty much without a hitch. Good luck, and next time don't be scared to ask yourself. I don't think this is a python problem, so I doubt this list is the correct place... -Lang Jake ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] togglebutton mouseover
Is there a way to make a ToggleButton not change state when moused over? I set the color of the button when clicked with a line like so: widget.modify_bg(gtk.STATE_ACTIVE, gtk.gdk.color_parse("light blue")) That works fine. On and off. Although you can't tell it's clicked until the mouse passes off of the button. In addition, when the mouse cursor passes over this button, it reverts to the unclicked gray color, until the mouse passes back off the button. If possible, I would like to have the button only change when clicked. The default seems very poor. Thanks -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] togglebutton mouseover
Sorry, I just realized I posted this to the wrong list. On 06/30/2010 12:20 AM, Lang Hurst wrote: Is there a way to make a ToggleButton not change state when moused over? I set the color of the button when clicked with a line like so: widget.modify_bg(gtk.STATE_ACTIVE, gtk.gdk.color_parse("light blue")) That works fine. On and off. Although you can't tell it's clicked until the mouse passes off of the button. In addition, when the mouse cursor passes over this button, it reverts to the unclicked gray color, until the mouse passes back off the button. If possible, I would like to have the button only change when clicked. The default seems very poor. Thanks -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Strange sqlite3 behavior
I just had the weirdest issue with sqlite3. I was trying to update a field to "Active". I have a little database of students and sometimes they get sent to juvi, or just check out for a couple of months and show back up. Anyway, I wanted to just have a field that had either "Active" or "Inactive". I know, could go all boolean, but wanted to leave my options open. Anyway, my code was along the lines of UPDATE students SET active="Inactive" where id="123456" That would work, but UPDATE students SET active="Active" where id="123456" wouldn't. It wouldn't do anything, the field still held "Inactive". I tried it manually through sqlite3browser and had the same result. Strange. UPDATE students SET active="Bob Sagat" where id="123456" worked. Anything but "Active" works. I was unable to update to "Active". You can imagine how frustrating this was. When I finally realized that it was only Active that didn't work, I changed my program to either "Active Student" or "Inactive Student" and it works fine. Just had to post this somewhere, after loosing even more hair. -Lang -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Strange sqlite3 behavior
On 07/17/2010 02:04 AM, Tim Golden wrote: On 17/07/2010 8:10 AM, Lang Hurst wrote: I just had the weirdest issue with sqlite3. I was trying to update a field to "Active". I have a little database of students and sometimes they get sent to juvi, or just check out for a couple of months and show back up. Anyway, I wanted to just have a field that had either "Active" or "Inactive". I know, could go all boolean, but wanted to leave my options open. Anyway, my code was along the lines of UPDATE students SET active="Inactive" where id="123456" That would work, but UPDATE students SET active="Active" where id="123456" wouldn't. It wouldn't do anything, the field still held "Inactive". My guess -- without bothering to check the sqlite docs -- is that sqlite uses single quotes to delimit strings and double quotes to delimit column. Or at least that it can be set up to do that. If that is the case then "Active" is taken to refer to the column active and not to the string active. So you're just setting it to itself. Try it with 'Active' instead. TJG Yep, you were correct. Just ran that through sqlite3browser and any phrase seems to work with either single or double quotes, EXCEPT "Active". 'Active' works though. Good call. I worked around it, but damn that was driving me crazy last night. -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] attachments?
Naw, didn't come through. On 07/21/2010 08:08 PM, Alex Hall wrote: -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] os.startfile
I have the following in my program: try: os.startfile('current_credit.txt') except: os.system('/usr/bin/xdg-open current_credit.txt') Basically, open a file in notepad if I'm on windows, vim if on my home linux computer. It works fine in linux and in Windows through virtualbox. The problem is when I take the program to work, it doesn't open the file. The computers at work are pretty locked down, so I'm thinking it has something to do with the os.startfile command. Are there any alternative commands I could try? -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] os.startfile
On 12/19/2010 01:16 PM, Steven D'Aprano wrote: Lang Hurst wrote: I have the following in my program: try: os.startfile('current_credit.txt') except: os.system('/usr/bin/xdg-open current_credit.txt') Basically, open a file in notepad if I'm on windows, vim if on my home linux computer. It works fine in linux and in Windows through virtualbox. The problem is when I take the program to work, it doesn't open the file. The computers at work are pretty locked down, so I'm thinking it has something to do with the os.startfile command. Are there any alternative commands I could try? You don't give us much information to go on, such as the version of Python you use, or the operating system on your work desktops, or the error that you see when you try this, or even if you can open the file by double-clicking it, but that's okay, because I love guessing games! I guess that the problem is that your work desktops are, in fact, Apple Macintoshes running OS-X. Am I close? Other than that, you shouldn't just blindly ignore the exception raised by startfile. Not all exceptions mean "You're not running Windows and there is no startfile command", and you shouldn't catch bare excepts. You would be better off doing this: try: os.startfile('current_credit.txt') except AttributeError: # No startfile command, so we're not on Windows. # Try a Linux command instead. # (Tested on Fedora, may not work on all distros.) os.system('/usr/bin/xdg-open current_credit.txt') That way, when you get a different error, like "file not found" or "permission denied", you will see what it is, and perhaps get a hint as to what the problem is. Python doesn't have super powers. If you can't open a file because the desktop has been locked down, then Python won't be able to magically open it. It has no more permissions to do things than you do. There's no magic command "open files even if I'm not allowed to open them". Sorry for the lack of information. I'm using Python 2.6.6, glade, gtk, vim. Once the program does what I want, I boot up the virtualbox image (XP) and try it in there. Usually it doesn't have a problem. If all works well, I wrap it all up into an executable using pyinstaller. Then I try to run the exe on XP. That works, so I pull it back into linux (Debian Sid, for what that's worth) and run the executable via wine. Everything checks out. I can't install anything at work (XP computer), hence the stand alone file. Then when I run it, everything works fine, except when I get to the point where I want notepad to open the file. I can browse to the file and manually open it with notepad and it's fine. It just won't open with notepad from the script. I just don't really know Windows that well. I was just wondering if my work computer being locked down like it is would stop the os.startfile command, and if so, do I have any alternative ways to do what I'm trying to do (open a text file). -- There are no stupid questions, just stupid people. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor