>from functools import partial

I use this  kind of explicit import for a few names that I use frequently,
namely defaultdict, contextmanager, everything from itertools...
I think of these as my personal extended set of builtins ;)

As to the actual partial() function, you probably don't see it a lot because
it has been in the standard library for only three years. The older idiom
for making a function that calls another function with a fixed argument is

command = lambda button=button: button_clicked(button)

Alright! What is a fixed argument?


You can iterate over (row, column) pairs instead of the dummy _ variable:

def create_widgets(self):
   >for row, column in [(0, 0), (1, 1), (2, 2), (3, 0)]:
       >button = tk.Button(self)
       >command = partial(button_clicked, button)
       >button["command"] = command
       >button.grid(row=row, column=column)
       >command()

Very nice! I didn't know that it was possible. This saves me a lot of space.
Am I, as a beginner, supposed to know this?

Say that when button one is pressed I want a text file to be created. The text in the file should be the same number as the button I pressed. So if I press button one, once, it should create a text file with the name button_1. The text in the file should also be "button_1".

If the button is pressed again, it should remove the file. Is this possible to do without
any trouble?

So if the second button is pressed , it should create a text file with the name button_2 and the text in it should be button_2. If the button is pressed once again, it is supposed to
delete that file it created.




I don't know if it works with this piece of code you posted earlier:

def create_widgets(self):
   >for row, column in [(0, 0), (1, 1), (2, 2), (3, 0)]:
       >button = tk.Button(self)
       >command = partial(button_clicked, button)
       >button["command"] = command
       >button.grid(row=row,



Otherwise it might work with the code below this comment?


def button_clicked(button):
   if button["bg"] == "green":
       button.configure(bg="red", text="Hi 2")
   else:
       button.configure(bg="green", text="Hi 1")

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

   def create_widgets(self):
       for _ in range(2):
           button = tk.Button(self)
           command = partial(button_clicked, button)
           button["command"] = command
           button.grid()
           command()





I also have one last question.
How do I make the first button that is created to be named
1, the second to be created 2, the third 3 and so on?






Also, assume that I have a already have a window with a button in it. If
you press this button, this window is supposed to open.
So, if I press the button, will this window open? Because I created the
previous window using
from tkinter import* and not import tkinter as tk.

You can make the decision what style you want to use on a per-module basis.
In that module you can then access (for example) a tkinter button with
either tkinter.Button, tk.Button or just Button.
You can even mix styles if you put the respective imports at the beginning
of the module (not recommended).
What approach you take has no consequences on the execution of the program.


I didn't know that, thanks. It is strange that this wasn't mentioned in my book. Why is it not recommended to to mix the styles if you put the respective imports at the
beginning of the module?


Thanks again for your help and insightful suggestions.
It is really appreciated :)

Mic


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to