我一直遇到tkinter的问题。 它应该在第一个Windows文本框中取数字 检查它是否可被5整除,如果是,则将其除以5并将其放在弹出窗口中。如果它不只是将数字放在弹出窗口上而不是将它除以5。
代码是这样的:
from tkinter import *
from tkinter import ttk
import tkinter as tk
answer = 0
def popup():
global answer
if (number[-1] == 5 or number[-1] == 0):
number / 5
if (int(number) > 1):
answer = "\number 5 cent coins"
else:
answer = "\number 5 cent coin"
else:
if (int(number) > 1):
answer = "\number 1 cent coins"
else:
answer = "\number 1 cent coin"
popup = tk.Tk()
popup.wm_title("answer")
popup.geometry("330x85")
answers = Label(popup, text=answer)
answers.pack
B1 = ttk.Button(popup, text="Ok", command=popup.destroy)
B1.pack()
def add_text():
global number
number = num_textbox.get()
print(number)
popup()
root.destroy()
root = Tk()
root.title("Number Cent Divider")
root.geometry("330x85")
num_col_mat = Label(root, text="Your number:")
num_col_mat.pack()
num_textbox = Entry(root, bd=1)
num_textbox.pack()
enter_button = Button(root, text="Enter", command=add_text)
enter_button.pack()
root.mainloop()
并且不会出现任何错误,但弹出窗口上的标签不会显示在:
popup = tk.Tk()
popup.wm_title("answer")
popup.geometry("330x85")
answers = Label(popup, text=answer)
answers.pack
B1 = ttk.Button(popup, text="Ok", command=popup.destroy)
B1.pack()
答案 0 :(得分:2)
您需要在标签对象上调用pack
,即在()
之后添加pack
。
因此,您打包answers
的地方应该是answers.pack()
。