Как я могу использовать grid_forget более одного раза в tkinter? - PullRequest
0 голосов
/ 11 февраля 2020

Я пытаюсь дать себе упражнения, используя импорт и храня программы и модули отдельно друг от друга. В этом конкретном упражнении, которое я дал себе, у меня есть конвертер базисных c футов в дюймы и дюймов в футы.

Все работает, кроме одной проблемы. Там, где ответ отображается в GUI, новый ответ покрывает старый ответ, а не заменяет его.

import tkinter as tk
from tkinter import E, W
import Converter

window = tk.Tk()
window.title("Converter")
answer_label = tk.Label(window, text="No Answer")
answer_label.grid(row=5)

def feet_to_inches_function(old_label):
    old_label.grid_forget()
    answer_label = tk.Label(window, text=Converter.feet_to_inches(int(entry_bar.get())))
    answer_label.grid(row=5)

def inches_to_feet_function(old_label):
    old_label.grid_forget()
    answer = tk.Label(window, text=Converter.inches_to_feet(int(entry_bar.get())))
    answer.grid(row=5)

title_label = tk.Label(window, text="Convert")
entry_bar = tk.Entry(window, font=('HELVETICA', 10))
fti_button = tk.Button(window, text="Feet to Inches", command=lambda: feet_to_inches_function(answer_label))
itf_button = tk.Button(window, text="Inches to Feet", command=lambda: inches_to_feet_function(answer_label))
quit_button = tk.Button(window, text="Quit", command=window.destroy)

title_label.grid(row=0, column=0, sticky=W)
entry_bar.grid(row=1, columnspan=2, sticky=(E, W))
fti_button.grid(row=3, column=0)
itf_button.grid(row=3, column=1)
quit_button.grid(row=4, columnspan=2, sticky=(E, W))

window.mainloop()

Также как отдельный вопрос мне трудно разобраться в mainl oop .Я знаю, что она превращает программу go в бесконечное число l oop, но откуда? Я не вижу никаких логических доказательств того, что мой код бесконечен l oop.

Большое спасибо

1 Ответ

0 голосов
/ 11 февраля 2020

Вот решение для вас:

import tkinter as tk
from tkinter import E, W


window = tk.Tk()
window.title("Converter")
answer_label = tk.Label(window, text="No Answer")
answer_label.grid(row=5)


def feet_to_inches_function():
    answer_label.configure(text='As you want (INCHES)')


def inches_to_feet_function():
    answer_label.configure(text='As you want (FEET)')


title_label = tk.Label(window, text="Convert")
entry_bar = tk.Entry(window, font=('HELVETICA', 10))
fti_button = tk.Button(window, text="Feet to Inches", command=feet_to_inches_function)
itf_button = tk.Button(window, text="Inches to Feet", command=inches_to_feet_function)
quit_button = tk.Button(window, text="Quit", command=window.destroy)

title_label.grid(row=0, column=0, sticky=W)
entry_bar.grid(row=1, columnspan=2, sticky=(E, W))
fti_button.grid(row=3, column=0)
itf_button.grid(row=3, column=1)
quit_button.grid(row=4, columnspan=2, sticky=(E, W))

window.mainloop()

Все, что вам нужно, это configure text Label в обеих функциях.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...