【问题标题】:Placing tkinter Label inside Frame [duplicate]将tkinter标签放在框架内[重复]
【发布时间】:2019-11-03 05:27:23
【问题描述】:

我一直在尝试将标签 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 帧下方。

【问题讨论】:

    标签: python tkinter


    【解决方案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)
    

    【讨论】:

    • 所以这都是由 .pack() 在同一行引起的。该死的,我会记住这一点的。
    【解决方案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")
    frame2.pack(fill=X)
    a = Label(frame2, text="Your score")
    b = Label(frame2, text="Computer score")
    a.pack()
    b.pack()
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 2020-07-02
      • 2021-09-27
      • 2021-04-22
      • 2017-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多