简体   繁体   中英

how to make a button with a different number in tkinter

im trying to name a button what it is named in ent and so it doesn't repeat when the button is pressed again so if you press once it's button1 and again button2

from tkinter import *

def ext():
    win1.destroy()
    but1 = Button(root, text=txt.get(), height=10, width=30)
    but1.grid(padx=3, row=0, column=1)



def create():
    global win1
    global txt
    win1 = Tk()
    win1.geometry("200x200")
    ent = Entry(win1)
    ent.pack(pady=20)
    txt = ent.get()
    sub = Button(win1, text="Submit", command=ext)
    sub.pack()



root = Tk()
root.geometry("750x750")
root.config(background="#6673ED")

create_but = Button(root, text="Create new card", height=10, width=30, command=create)
create_but.grid(row=0,column=0)

root.mainloop()

The code below uses a dictionary of lists to add an integer to any repeating text input. I think this is what the question is about.

import tkinter as tk  # import as tk is safer and more flexible

# Globals to keep it simple
names = {}  # Dictionary.  Will become a dictionary of lists.
row = 1
col = 0
win1 = None
ent = None

def add_text( txt ):
    """  Adds txt to the names dictionary if it doesn't already exist.
         Adds and integer to the txt if it does already exit """ 
    name = names.get( txt, None )
    if name:
        name.append( txt + str(len( name )) )  # Append `txt + int` to a list
    else:
        names[ txt ] = [ txt ]    # Add a list of one item to the dict.
    # print( names )   # Uncomment to see what is happening.
    return names[ txt ][-1]


def ext():
    global row, col
    txt = ent.get()   # Get the text from the entry 
    win1.destroy()    # before the window is destroyed
    txt = add_text( txt )
    but1 = tk.Button(root, text=txt, height=10, width=30)
    but1.grid(padx=3, row=row, column=col)   # row and column need to 
                                             # change to show all buttons.
    col += 1
    if col > 2:
        row += 1
        col = 0

def create():
    global win1
    global ent
    win1 = tk.Toplevel()   # Create a second window with tk.Toplevel.  
                           # Never create two tk.Tk objects. 
    win1.geometry("200x200")
    ent = tk.Entry(win1)
    ent.pack(pady=20)
    ent.focus()       # Position the focus in the Entry
    # txt = ent.get() # removed as the Entry was being read before data was entered.
                      # The entry is now read in `ext`.
    sub = tk.Button( win1, text="Submit", command=ext )
    sub.pack()

root = tk.Tk()
root.geometry("750x750")
root.config(background="#6673ED")

create_but = tk.Button(root, text="Create new card", height=10, width=30, command=create)
create_but.grid(row=0,column=0)

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM