我是tkinter和编码的新手,我想知道为什么我看不到其余的按钮。有人能帮我吗?我是编码时的总菜鸟。
from Tkinter import*
x=Tk()
def message():
y=Tk()
y.geometry("375x500")
label=Label(y,bg="light green")
recip=Entry(y)
recip.place(x=90, y=20, relwidth=0.7, relheight=0.04)
to=Label(y,text="To:",bg="maroon", fg="light green", )
to.place(x=15,y=20, relwidth=0.2,relheight=0.04)
chat=Entry(y)
chat.place(x=15,y=300, relwidth=0.7,relheight=0.05)
send=Button(y,text="send", bg="maroon", fg="light green")
send.place(x=275,y=300, relwidth=0.2,relheight=0.05)
q=Button(y,text="Q")
q.place(x=15,y=330)
w=Button(y,text="W")
w.place(x=35,y=330)
e=Button(y,text="E")
e.place(x=55,y=330)
r=Button(y,text="R")
r.place(x=75,y=330)
t=Button(y,text="T")
t.place(x=95,y=330)
y=Button(y,text="Y")
y.place(x=115,y=330)
u=Button(y,text="U")
u.place(x=15,y=330)
i=Button(y,text="I")
i.place(x=155,y=330)
o=Button(y,text="O")
o.place(x=175,y=330)
p=Button(y,text="P")#10
label.pack(expand=True, fill=BOTH)
y.mainloop()
button=Button(x,text="sample", command=message)
button.pack()
x.mainloop()
答案 0 :(得分:1)
这是因为你不能有两个主窗口,例如。 TK()。使用Toplevel()创建另一个窗口。
你还坚持使用一个几何管理器,例如。包/地点/格。改变任何地方包装或反过来。此外,按钮p
根本没有放置。我意识到这是一些问题,但它们很重要。查看其他两个答案来修复按钮。
答案 1 :(得分:0)
Lafexlos所述的问题是,您要覆盖Tk()
的实例以添加按钮。
我相信你应该动态创建你的按钮,而不是单独做每一个。然后给他们一个命令,返回相应的字母。
我使用了pack而不是place,因为我认为它最适合这种程序如何创建按钮。您还需要确保不要在同一个grid, place and/or pack
实例中使用Tk()
,否则会发现这会导致问题。
import tkinter as tk
def open_message():
message = tk.Toplevel(root, bg = "light green")
message.minsize(300, 400)
keys = 'QWERTYUIOPASDFGHJKLZXCVBNM'
# frames for the keyboard
keyboard = tk.Frame(message, bg = "light green")
row1 = tk.Frame(keyboard)
row2 = tk.Frame(keyboard)
row3 = tk.Frame(keyboard)
row1.pack()
row2.pack()
row3.pack()
# Dynamically creates each button
# position of button depends on key index
# using index the button is assinged to the relative row
# this only includes letters. To add more, add to keys string
# and change the values to determine the assigned row
for idx, letter in enumerate(keys):
if idx < 10:
btn = tk.Button(row1, text=letter, command=lambda i=idx: get_letter(i))
elif idx < 19:
btn = tk.Button(row2, text=letter, command=lambda i=idx: get_letter(i))
else:
btn = tk.Button(row3, text=letter, command=lambda i=idx: get_letter(i))
btn.pack(side = tk.LEFT)
to_frame = tk.Frame(message, bg = "light green", padx = 40)
send_frame = tk.Frame(message, bg = "light green", padx = 40)
tk.Label(to_frame, text="To:", bg="maroon", fg="light green", relief = tk.RAISED).\
pack(side = tk.LEFT, ipady = 3)
recip = tk.Entry(to_frame)
recip.pack(side = tk.LEFT, fill = tk.BOTH, expand=True)
chat = tk.Entry(send_frame)
chat.pack(side = tk.LEFT, fill = tk.BOTH, expand=True)
send = tk.Button(send_frame, text="send", bg="maroon", fg="light green")
send.pack(side = tk.LEFT)
to_frame.pack(fill = tk.X, expand=True)
# I imagine your text widget would be packed here
keyboard.pack()
send_frame.pack(fill = tk.X, expand=True)
# Nested function to print values using keys
def get_letter(i):
# will print the letter depending on the button pressed
print(keys[i])
root = tk.Tk()
button = tk.Button(root,text="sample", command = open_message)
button.pack()
root.mainloop()