简体   繁体   中英

How to resize tkinter window to minsize value, after label is destroyed

I'm trying to make the window go back to it's original size after the label is destroyed, so it looks neater, how do I do that? Thanks in advance. So the code is for a binary converter, using tkinter. It has binary to text and text to binary functions. I want to add the function described in the first line into the clear result button function. This is the code below:


def clear_result():
    for w in result.winfo_children():
        w.destroy()

    window.minsize(420, 80)

    entry.delete(0, tk.END)

def binary_to_text(convert):
    for w in result.winfo_children():
        w.destroy()

    window.minsize(420, 80)

    # the line below is copy and pasted, just to let you know, I'm probably not as good as python as you think I am
    ascii_string = "".join([chr(int(binary, 2)) for binary in convert.split(" ")])
    tk.Label(result, text=ascii_string).pack()

def text_to_binary(convert):
    # clear existing result
    for w in result.winfo_children():
        w.destroy()

    window.minsize(420, 80)

    for x in convert:  # iterates over each character in the list
        ascii_values = ord(x)  # gets the ascii value for x
        binary = format(ascii_values, '08b')
        tk.Label(result, text=f'{x} {binary}').pack()

# mainline

window = tk.Tk()
window.title('Binary Converter')

window.minsize(420, 80)

entry = tk.Entry(window, width=25)
entry.grid(row=0, column=0)

# frame for the conversion result
result = tk.Frame(master=window)
result.grid(row=2, column=0)

btn_clear_result = tk.Button(master=window, relief=tk.RIDGE, text='Clear results', command=lambda: clear_result())
btn_clear_result.grid(row=1, column=0)

btn_binary_convert = tk.Button(master=window, relief=tk.RIDGE, text='Convert the binary to text', command=lambda: binary_to_text(entry.get()))
btn_binary_convert.grid(row=0, column=1)

btn_text_convert = tk.Button(master=window, relief=tk.RIDGE, text='Convert the text to binary', command=lambda: text_to_binary(entry.get()))
btn_text_convert.grid(row=1, column=1)

window.mainloop()

You can remove the result frame using grid_remove() and put it back when showing conversion result:

def clear_result():
    for w in result.winfo_children():
        w.destroy()
    result.grid_remove()  # remove the frame

    entry.delete(0, tk.END)

def binary_to_text(convert):
    for w in result.winfo_children():
        w.destroy()

    # the line below is copy and pasted, just to let you know, I'm probably not as good as python as you think I am
    ascii_string = "".join([chr(int(binary, 2)) for binary in convert.split(" ")])
    tk.Label(result, text=ascii_string).pack()

    result.grid()  # put back the frame


def text_to_binary(convert):
    # clear existing result
    for w in result.winfo_children():
        w.destroy()

    for x in convert:  # iterates over each character in the list
        ascii_values = ord(x)  # gets the ascii value for x
        binary = format(ascii_values, '08b')
        tk.Label(result, text=f'{x} {binary}').pack()

    result.grid()  # put back the frame

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