将tkinter标签放置在框架内

时间:2019-11-03 05:27:23

标签: python tkinter

我一直试图将标签a,b放在第2帧内。

import random
from  tkinter import *

root = Tk()

root.title("Rock Paper Scissor")

root.geometry("600x600")

# user choice frame
frame1 = Frame(root, height=200, bg="cyan")
frame1.pack(fill=X)
# frame1.pack_propagate(0) # stop frame from force fit

Button(frame1, text="ROCK").pack(side=TOP)
Button(frame1, text="PAPER").pack(side=TOP)
Button(frame1, text="SCISSOR").pack(side=TOP)

# score frame
frame2 = Frame(root, height=50,bg="orange").pack(fill=X)
a = Label(frame2, text="Your score")
b = Label(frame2, text="Computer score")
a.pack()
b.pack()

root.mainloop()

但是只要我运行此代码,标签a和b就会显示在第2帧下方。

2 个答案:

答案 0 :(得分:1)

尝试以下代码。

import random
from  tkinter import *

root = Tk()

root.title("Rock Paper Scissor")

root.geometry("600x600")

# user choice frame
frame1 = Frame(root, height=200, bg="cyan")
frame1.pack(fill=X)
# frame1.pack_propagate(0) # stop frame from force fit

Button(frame1, text="ROCK").pack(side=TOP)
Button(frame1, text="PAPER").pack(side=TOP)
Button(frame1, text="SCISSOR").pack(side=TOP)

# score frame
frame2 = Frame(root, height=50,bg="orange")
frame2.pack(fill=X)
a = Label(frame2, text="Your score")
b = Label(frame2, text="Computer score")
a.pack()
b.pack()
root.mainloop()

答案 1 :(得分:1)

此问题已经得到解答here

Frame(root, height=50,bg="orange").pack(fill=X)不返回框架,而是返回None。因此,您的frame2始终是None

您应该执行与frame1相同的操作。

frame2 = Frame(root, height=50, bg="orange")
frame2.pack(fill=X)