【问题标题】:Pack a text widget on the side doesn't work在侧面打包文本小部件不起作用
【发布时间】:2018-08-25 04:45:15
【问题描述】:

我对@9​​87654321@ 有点陌生。我尝试在左侧的0,0 创建一个Text 小部件,但它出现在中间,就像默认的pack()

这是我的代码:

from Tkinter import *


# the ui of the main window
class Ui(object):
    # the init of the client object
    def __init__(self):
        self.root = Tk()
        self.mid_height = self.root.winfo_screenheight() / 2
        self.mid_width = self.root.winfo_screenwidth() / 2
        self.root.title("Journey-opening")
        self.root.geometry("600x600+{}+{}".format(self.mid_width - 300, self.mid_height - 300))
        self.root.resizable(width=0, height=0)
        self.cyan = "#0990CB"
        self.root["background"] = self.cyan
        self.frame = Frame(self.root)
        self.frame.pack()
        self.chat_box = Text(self.frame, height=30, width=50)
        self.chat_box.pack(side=LEFT)

    def open(self):
        self.root.mainloop()

wins = Ui()
wins.open()

我也尝试了grid 方法,但它没有改变任何东西,并且还创建了另一个小部件,因为它可能至少需要 2 个小部件。

我猜它与我的框架有关,但我按照教程进行操作,一切似乎都很好。

【问题讨论】:

    标签: tkinter python python-2.7 layout tkinter


    【解决方案1】:

    “在侧面打包一个文本小部件不起作用”

    这是不正确self.chat_box.pack(side=LEFT) 确实Text小部件打包到一边。只是它在 inside self.frame 中完成,默认情况下它会为它封装的小部件(在这种情况下只是文本小部件)分配 精确 所需的空间。所以在某种程度上,Text 小部件被打包,不仅在左侧,而且在所有边


    为了让self.chat_box在左上角,你应该让frame占据比需要更多的空间,在这种情况下,它可以简单地占据x轴里面的所有空间它的父母(self.root)。为此,请替换:

    self.frame.pack()
    

    与:

    self.frame.pack(fill='x') # which is the same as self.frame.pack(fill=X)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-14
      • 1970-01-01
      • 2020-07-08
      • 2019-03-31
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 1970-01-01
      相关资源
      最近更新 更多