[Tutor] Shortening the code

Mic o0MB0o at hotmail.se
Sat Nov 26 16:08:32 CET 2011


>> Alright! By the way, it seems like some people favour the use of 
>> pastebins,
>> while others don?t, which is the way to go and why?

>I've stated my preference, Steven has stated his, so I guess you need to
>decide for yourself. However the best bet is not to paste long pieces of
>code at all, but stick to samples. They are easier to read and therefore
>more likely to get a reply...

Alright, I get it, the safe way would be to just post small pieces of code 
here.


>How are you running the code?
>From a command prompt or inside an IDE?

I am running it inside an IDLE. Does it matter whether I run it in a command 
promt
or in an IDLE?


>> Assuming you feel that it is time for do this, how do I implement the
>> creation of these text files into the code that we discussed earlier?

>The easiest way is just to use regular Python data structures in a
>separate python file. Then you just import that file. So using your
>chair_list, you would put that in a sepasrate file like:


>list_chair = [
>#row, col, span, name
>(0, 1, 0, '01'),
>(0, 2, 0, '02'),
>(0, 3, 0, '03'),
>(0, 4, 0, '04'),
>(1, 1, 2, '05'),
>(1, 3, 2, '06')
>]

>Assume you call it chair_config.py

>You can then import the data with

>from chair_config import list_chair

>And the rest of your code remains the same.


Yes I understand that :)
I think that I was unclear perhaps about what I meant with text files. 
Therefore I will post an example
of my code. While it works fine, I think it would be good if we could 
implement it into your suggestions!
I have marked the part of the code I have been talking about with ##########
Here is the code:

import tkinter as tk
import os

FREE="green"
OCCUPIED="red"

class SeatButton(tk.Button):
            def __init__(self, master, index):
                text = "{}".format(index+1)
                super(SeatButton, self).__init__(master, text=text, bg=FREE,
command=self.clicked)
                self.filename = "Germany_France{}.txt".format(index+1)
                self.occupied = False



            def clicked(self):
                self.occupied = not self.occupied
                if self.occupied:
                    self["bg"] = OCCUPIED
                    ###################################
                    text_file=open(self.filename,"w")
                    text_file.write(self.filename)
                    text_file.close
                    ###################################
                else:
                    self["bg"] = FREE
                    ################################
                    os.remove(self.filename)
                    #################################



class Window(tk.Frame):
        def __init__(self, master):
            super (Window, self).__init__(master)
            self.grid()
            self.create_widgets()

        def create_widgets(self):
            for index in range(20):
                button = SeatButton(self, index)
                row, column = divmod(index, 4)
                button.grid(row=row, column=column)

root = tk.Tk()
root.title("Test")
app = Window(root)
root.mainloop()



As you can see, if button one it pressed it creates a file with the name 
Germany_France1
and with the contents Germany_France1 . If it is pressed once more, it 
removes the file.

If button two is pressed it creates a file with the name Germany_France2 and 
with the contents
Germany_France2. If it is pressed once more, it removes the file.

And so on.


I wonder, do you know how I can do this in your piece of code? Is it 
possible to do without any trouble?
While this piece of code is very good, I feel that I have it easier to 
understand your code.
Perhaps it was I who misunderstood you, sorry if that is the case, but 
otherwise this is what I want the
code to do!


Thanks!

Mic



More information about the Tutor mailing list