【问题标题】:Tkinter tk.Scrollbar Grid and alignment issuesTkinter tk.Scrollbar 网格和对齐问题
【发布时间】:2019-06-16 16:02:59
【问题描述】:

我在 Tkinter 中创建了一个文本字段,在文本字段旁边添加了新项目,但我无法通过网格获得底线。添加网格时,窗口不会打开。解决办法是什么?

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import tkinter as tk

pencere = tk.Tk()

frame = tk.Frame(pencere)
frame.pack()

scrol = tk.Scrollbar(frame)
scrol.pack(side=tk.RIGHT, fill=tk.Y)

text = "Good bye"

text = tk.Text(frame,width=70, height=0)
text.insert(tk.INSERT, text)
text.config(yscrollcommand=scrol.set)
scrol.config(command=text.yview)
text.pack(side=tk.LEFT, fill=tk.Y)

buton = tk.Button(frame, text='close', command=pencere.destroy)
buton.pack()

text.grid(row=1, column=1)

pencere.mainloop()

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:

    您不能对具有相同 master 的小部件使用 packgrid 方法。在此示例中,您的框架、文本和滚动条已经由 pack 调整,因此 tkinter 不允许使用 grid 调整文本或其他元素,只要您不这样做为要使用 grid 调整的元素创建另一个框架。

    所以,我认为有两种选择:

    1. 如果您仍想使用 grid,则应使用网格调整整个小部件(框架、文本等)。
      1. 如果您想使用 grid 但又不想更改已经通过 pack 调整的小部件,请为您的文本创建一个新框架。

    【讨论】:

      【解决方案2】:

      亲爱的 Zolo Baba,该栏无法出现,因为您仍在同时使用“pack”和“grid”。由于我上面提到的原因,我们只能对master使用一种布局管理方法。我只使用“网格”方法编辑了您的代码。

      #!/usr/bin/python3
      # -*- coding: utf-8 -*-
      
      import tkinter as tk
      
      pencere = tk.Tk()
      
      frame = tk.Frame(pencere)
      frame.grid()
      
      scrol = tk.Scrollbar(frame, orient = tk.VERTICAL)
      text = tk.Text(frame,width=70, height=5)
      
      text.insert(tk.INSERT, "###\n\n\nNote\n\n\n###")
      text.config(yscrollcommand=scrol.set)
      text.grid(sticky="W")
      
      scrol.config(command=text.yview)
      scrol.grid(row = 0, column=2, sticky="NS")
      
      buton = tk.Button(frame, text='close', command=pencere.destroy)
      buton.grid(row = 0, column = 1)
      
      pencere.mainloop()
      

      【讨论】:

        猜你喜欢
        • 2021-01-12
        • 1970-01-01
        • 2018-10-03
        • 2014-12-25
        • 1970-01-01
        • 1970-01-01
        • 2016-08-18
        相关资源
        最近更新 更多