Кто-нибудь знает, почему мои кнопки tkinter не рендерится? - PullRequest
0 голосов
/ 27 марта 2019

Это для проекта Python на GitHub, где я делаю графический интерфейс для симуляции Magic 8 Ball. Кажется, я не могу использовать функцию .pack (), или мое окно просто загружается навсегда, даже не создавая экземпляров.

При создании

Когда я нажимаю кнопку, появляется текст

window = Tk()
window.configure(bg="black")
window.title("Magic 8 Ball")
Label(window, text="Ask the Magic 8 Ball the question on your mind or enter X to exit: ", bg="black", fg="white")\
    .grid(row=0, column=0)


# Create entry box to type question
entrybox = Entry(window, width=30, bg="white")
entrybox.grid(row=1, column=0)


# Create output box at below buttons
output = Text(window, bg="white", fg="black", width=40, height=5)
output.grid(row=4, column=0)


# Create 4 button: Ask, Clear, Play Again, Quit
button_frame = Frame(window)
button_frame.configure(bg="black")
button_frame.grid(row=2, column=0)

#button_frame.pack(fill=X, side=BOTTOM)
Button(button_frame, text="Ask", width=10, bg="black", fg="white", command=click).grid(row=2, column=0)
Button(button_frame, text="Clear", width=10, command=clear).grid(row=2, column=1)
Button(button_frame, text="Play Again", width=10,command=repeat).grid(row=3, column=0)
Button(button_frame, text="Quit", width=10, command=close).grid(row=3, column=1)


window.mainloop()

1 Ответ

0 голосов
/ 27 марта 2019

Я думаю, нам понадобятся дополнительные спецификации о вашей ОС и версии Python. Я запустил его на Python 3.5.0 и Windows 10 с результатом:

Buttons are visible

Так что я не думаю, что это ошибка в вашем коде. Мне пришлось добавить все функции и импортировать, что вы пропустили, так что то, что я запустил, в итоге выглядело так:

from tkinter import *
window = Tk()
def click():
    print('click')
def clear():
    print('clear')
def repeat():
    print('repeat')
def close():
    print('close')
window.configure(bg="black")
window.title("Magic 8 Ball")
Label(window, text="Ask the Magic 8 Ball the question on your mind or enter X to exit: ", bg="black", fg="white")\
    .grid(row=0, column=0)


# Create entry box to type question
entrybox = Entry(window, width=30, bg="white")
entrybox.grid(row=1, column=0)


# Create output box at below buttons
output = Text(window, bg="white", fg="black", width=40, height=5)
output.grid(row=4, column=0)


# Create 4 button: Ask, Clear, Play Again, Quit
button_frame = Frame(window)
button_frame.configure(bg="black")
button_frame.grid(row=2, column=0)

#button_frame.pack(fill=X, side=BOTTOM)
Button(button_frame, text="Ask", width=10, bg="black", fg="white", command=click).grid(row=2, column=0)
Button(button_frame, text="Clear", width=10, command=clear).grid(row=2, column=1)
Button(button_frame, text="Play Again", width=10,command=repeat).grid(row=3, column=0)
Button(button_frame, text="Quit", width=10, command=close).grid(row=3, column=1)


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