Re: [Tutor] Shortening the code of a finsihed program.
On 26/11/11 00:07, Steven D'Aprano wrote: Its usually better to paste long programs into a pastebin web site and give us a link. I'd just like to say that some of us disagree with this advice. Some people (e.g. me) often read their where getting access to a browser is less convenient. Its true there are downsides to every option. Even a 200 line program isn't that big, it's only about 6K. That adds up if you are reading on a 3G smartphone with a 100M data limit or paying by the byte. And attachments don't help there. Plus many email gateways block all attachments as a security measure. is so large that syntax highlighting becomes particularly useful, then chances are it's too long and people won't read it regardless. I find syntax highlighting useful even in very short snippets of code, but I agree it's better to just keep the code sample short and post it inline. And I do take the point that pastebins are short lived and so unless the replies incorporate the defective code snippets its lost to the archive. Nonetheless I personally prefer a pastebin posting to a long mail listing with no highlighting and often defective indenting. as I say there is no perfect solution to long posts other than to shorten them -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Shortening the code of a finsihed program.
Hello Mic, Mic wrote: Its usually better to paste long programs into a pastebin web site and give us a link. This saves cluttering up people mail with long messages, and also the pastebin rendering will usually be easier to read with syntax coloring etc. Please keep a attribution line when quoting people directly. Even something simple like "Fred wrote" will do. Alright. Sorry if I should know this, but what is a pastebin web site and how do I paste my program into a pastebin web site? Here's one: http://pastebin.com/ But as I've just written in another post, please don't use it :) While its perfectly legal Python to create a class inside a method its very unusual in practice and very restricting in the use of the class. Its nearly always better to declare all your classes at the top level of the program. Why is it restricting? Because generally speaking, if you define a class inside a function (or method), you can't use it *except* inside that function or method. class K: def method(self, x): class Q: pass instance = Q() This doesn't work because Q is not visible from the outside of K.method. Sometimes this is what you want; generally it is not. I don't know much about tkinter, so I will avoid commenting about your code. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Shortening the code of a finsihed program.
Alan Gauld wrote: On 24/11/11 16:20, Mic wrote: and then try to put these parts togheter into a large program, I decided to post my entire program. Its usually better to paste long programs into a pastebin web site and give us a link. This saves cluttering up people mail with long messages, and also the pastebin rendering will usually be easier to read with syntax coloring etc. I'd just like to say that some of us disagree with this advice. Some people (e.g. me) often read their where getting access to a browser is less convenient. More importantly, many pastebin sites are short-term only, after some weeks the bin disappears and the code is lost, which destroys the archival value of the post. If your code snippet is short, say, less than two dozen lines, there's no need to using a pastebin, just include it in your post in-line. More than that, you should attach it to the post as an attachment, as a .txt or .py file. Under no circumstances use your mail client's "rich text", colours or multiple fonts. Even a 200 line program isn't that big, it's only about 6K. If your code is so large that syntax highlighting becomes particularly useful, then chances are it's too long and people won't read it regardless. We're volunteers doing free support, but there are limits to how much code we're going to wade through if it doesn't interest us personally. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Shortening the code of a finsihed program.
On 25/11/11 21:14, Mic wrote: Alright. Sorry if I should know this, but what is a pastebin web site and how do I paste my program into a pastebin web site? A web site that you can paste stuff and then provide a link(url) that others can use to view it. You can usually specify the code style and it will apply syntax coloring for you. Try a google search for free pastebin... As an example I've pasted your code from this message at: http://pastebin.com/H3VzaapV Take a look and you will see what I mean. While its perfectly legal Python to create a class inside a method its very unusual in practice and very restricting in the use of the class. Why is it restricting? Because the class is only available inside the function. You cannot create objects of that class anywhere outside the class. So I figured I could post it here (short code) and ask you a couple of questions regarding the code: import tkinter as tk from functools import partial def button_clicked(button): if button["bg"] == "green": button.configure(bg="red", text="01") else: button.configure(bg="green", text="01") Note you are setting text to '01' in every case. Thats probably not what you want? def create_widgets(self): list_chair=[(0, 0), (0, 1), (0, 3), (0, 4), (1,0)] for row, column in list_chair: button = tk.Button(self) command = partial(button_clicked, button) button["command"] = command button.grid(row=row, column=column) command() As stated above this will result in every chair being green and having the text '01' It would be better to put the initial colour and text in your data list and configure each button directly: def create_widgets(self): list_chair=[(0, 0, '01'), (0, 1, '02'), (0, 3, '03'), (0, 4, '04'), (1, 0, '05')] for row, column, name in list_chair: command = partial(button_clicked, button) button = tk.Button(self, color='green', command=command, text=name) button.grid(row=row, column=column) Incidentally, This is what I mentioned early on in the discussion about using a data table to create your widgets. root = tk.Tk() root.title("Test") root.geometry("200x200") app = Window(root) root.mainloop() --When the first button is pressed I want a file to be created with the name Germany_France_1. The text in the file should be Germany_France_1. If/When the button is pressed again it should delete the file. Lets get the initial UI set up correctly first. Once we have done that we can worry about adding the functionality. This is one of the good habits in programming. Do it step by step. Get one thing working first before trying to add more features. Its a lot easier to debug code when you know what you changed to stop it working. Do you have any idea on how I can accomplish this? I reached the conclusion that it should be easy to do so since it was so easy > to create so many buttons in so little amount of code. Yes, but lets get the UI all done first, then we can add the button features. You should only ever have one Tk() object in a Tkinter program. Why is that? Because thats how Tkinter expects to work! It builds a tree of all the windows and widgets in your program. If you have two trees in the same program it can get very confused about which widget is doing what to which other widgets, especially if they wind up in different trees!. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Shortening the code of a finsihed program.
Its usually better to paste long programs into a pastebin web site and give us a link. This saves cluttering up people mail with long messages, and also the pastebin rendering will usually be easier to read with syntax coloring etc. Alright. Sorry if I should know this, but what is a pastebin web site and how do I paste my program into a pastebin web site? While its perfectly legal Python to create a class inside a method its very unusual in practice and very restricting in the use of the class. Its nearly always better to declare all your classes at the top level of the program. Why is it restricting? Because you post lost all the formatting, I'm not sure where this code is supposed to sit... This is again where a pastebin would help. Oh, now I understand why I shouldn't post like this. My apologize, won't happen again. And this is repeating all the bad habits from your original posts. If you adopt the ChairButton class approach all of this becomes much clearer and simpler. I got a really good tip earlier on in the thread how I could make my program dramatically shorter. I have tried to implement a couple of things I need into that piece of code, but I cannot get it to work. So I figured I could post it here (short code) and ask you a couple of questions regarding the code: import tkinter as tk from functools import partial def button_clicked(button): if button["bg"] == "green": button.configure(bg="red", text="01") else: button.configure(bg="green", text="01") class Window(tk.Frame): def __init__(self, master): super (Window, self).__init__(master) self.grid() self.create_widgets() def create_widgets(self): list_chair=[(0, 0), (0, 1), (0, 3), (0, 4), (1,0)] for row, column in list_chair: button = tk.Button(self) command = partial(button_clicked, button) button["command"] = command button.grid(row=row, column=column) command() root = tk.Tk() root.title("Test") root.geometry("200x200") app = Window(root) root.mainloop() Questions: --I want the first button to be created to have the text "1". The second should have the text "2". The third the text "3" and so on. --When the first button is pressed I want a file to be created with the name Germany_France_1. The text in the file should be Germany_France_1. If/When the button is pressed again it should delete the file. When the second button is pressed I want a file to be created with the name Germany_France_2. The text in the file should be Germany_France_2. If/When the button is pressed again it should delete the file. When the third button is pressed I want a file to be created with the name Germany_France_3. The text in the file should be Germany_France_3. If/When the button is pressed again it should delete the file. Do you have any idea on how I can accomplish this? I reached the conclusion that it should be easy to do so since it was so easy to create so many buttons in so little amount of code. However, to my dismay, I have tried this entire evening without success. It feels like I am so close to shorten my program from 2k+ lines of code to maybe 250 at most, but still so far away. You should only ever have one Tk() object in a Tkinter program. Why is that? And again you should only have one mainloop running, otherwise things would get very confusing with events winding up in the wrong windows event queue etc. Alright, my teacher never mentioned that when he examined my program. Thanks for your input! Your help is as always appreciated and my apologize for making confusing posts! Mic ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Shortening the code of a finsihed program.
On 24/11/11 16:20, Mic wrote: and then try to put these parts togheter into a large program, I decided to post my entire program. Its usually better to paste long programs into a pastebin web site and give us a link. This saves cluttering up people mail with long messages, and also the pastebin rendering will usually be easier to read with syntax coloring etc. achieve. I apologize if my English is flawed, but I hope it is understandable. Your English is excellent and no barrier to understanding what you are trying to do. #Importing necessary modules. from tkinter import* import os chair1_color="green" chair1_value=False chair2_color="green" chair2_value=False You already got some help on how to avoid these global variables by creating a subclass of Button. These then become two attributes of the new ChairButton class #Creates the Main Menu Window. class Main_Menu_Window(Frame): def __init__(self,master): super(Main_Menu_Window,self).__init__(master) self.grid() self.create_mainmenu_widgets() #Creates the widgets in the window. def create_mainmenu_widgets(self): self.instructions=Label(self, text="Welcome to our online booking system") self.instructions.grid() self.to_booking_window=Button(self, text="Book/Unbook your ticket", command=self.book_unbookmode) self.to_booking_window.grid() self.exit_program=Button(self, text="Exit Program", command=self.exit_program) self.exit_program.grid() #Creates a method that quits the program if the user presses the button "Exit Program" def exit_program(self): root.destroy() #Creates a new window where you book your tickets if you press the # button "Book/Unbook your ticket". def book_unbookmode(self): class BookOrUnbookTicketWindow(Frame): While its perfectly legal Python to create a class inside a method its very unusual in practice and very restricting in the use of the class. Its nearly always better to declare all your classes at the top level of the program. def __init__(self,master): super(BookOrUnbookTicketWindow,self).__init__(master) self.grid() self.create_book_unbookmode_widgets() #Creates widgets to place in the BookOrUnBookTicketWindow. def create_book_unbookmode_widgets(self): self.instructions_bookticket=Label(self, text="""To book a chair, click on a green chair. It then turns red, which means that you have booked the chair. if you wish to unbook your chair, press the red button once. It will then turn green, which means that you have unbooked your ticket, and the chair is now bookable again.""") self.instructions_bookticket.grid() #Creates two chairs. It should be more, but this is just used as an example. To get more chairs, this code in the section below #this comment will need to be shortened somehow, otherwise the code would be way too long. Because you post lost all the formatting, I'm not sure where this code is supposed to sit... This is again where a pastebin would help. self.chair1=Button(self, bg=chair1_color, text="01", command=self.chair1_clicked) self.chair1.grid() self.chair2=Button(self, bg=chair2_color, text="02", command=self.chair2_clicked) self.chair2.grid() def chair1_clicked(self): """ This method runs if chair one is clicked""" def change_chair1_value(): global chair1_value chair1_value=not chair1_value And this is repeating all the bad habits from your original posts. If you adopt the ChairButton class approach all of this becomes much clearer and simpler. change_chair1_value() if chair1_value: self.chair1.configure(bg="Red") text_file=open("New_York_Canada_15_00_Chair1","w") text_file.write( "New York-Canada\nSeat:1")#Notice that I want the Seat number written into the #textfile should be 1, if you press chair one, #2 if you press chair 2, 3 if you press chair 3 and so on. #However, the text should be "New York-Canada should # be the same for all chairs. text_file.close() def change_chair1_color_red(): global chair1_color chair1_color=("red") change_chair1_color_red() else: self.chair1.configure(bg="green") os.remove ("New_York_Canada_15_00_Chair1") #The file is supposed to be removed because #it means that you have unbooked your ticket. def change_chair1_color_green(): global chair1_color chair1_color=("green") change_chair1_color_green() #- def chair2_clicked(self): """ Ths method runs if chair two is clicked""" def change_chair2_value(): global chair2_value chair2_value=not chair2_value change_chair2_value() if chair2_value: self.chair2.configure(bg="Red") text_file=open("New_York_Canada_15_00_Chair2","w")#The file name should end with 1 if chair one is pressed. #if chair2 is pressed, it should end with chair2. If chair three is #pressed it should end with