简体   繁体   English

ScrolledText没有出现在Tkinter网格布局中

[英]ScrolledText not appearing with tkinter grid layout

When using .pack() with my ScrolledText widget for tkinter the proper box appears with a textbox that has a scrollbar. 当将.pack()ScrolledText小部件一起使用以进行.pack() ,将出现带有带有滚动条的文本框的正确框。 However when I switch it to use .grid() it no longer appears in the window. 但是,当我将其切换为使用.grid()它不再出现在窗口中。 Just is gone. 只是走了。

Here is what the applicable part of my code looks like: 这是我代码的适用部分:

import tkinter as tk
import tkinter.scrolledtext as tkst
import tkinter.font as tkFont
import tkinter.ttk as ttk
class Application(tk.Frame):

def __init__(self, master=None):
    tk.Frame.__init__(self, master)
    # self.grid()
    self.createWidgets()

def createWidgets(self):
    self.logArea = tkst.ScrolledText(self,
                wrap   = tk.WORD,
                width  = 20,
                height = 8,
                state="disabled",
                name="logArea"
            )
            self.logArea.grid(padx=10, pady=10,row=1, sticky=tk.W)

root = tk.Tk()
app = Application(master=root)
app.mainloop()

I have a Treeview widget on row 1 which displays fine, just for some reason this ScrolledText widget does not display at all, but worked fine when I was using just pack() before. 我在第1行上有一个Treeview小部件,该小部件显示得很好,只是出于某种原因,这个ScrolledText小部件根本不显示,但是在我之前只使用pack()时工作正常。

Any idea what is wrong? 知道有什么问题吗?

Edit: I added a self.master = master to the init and then used self.master instead of just self within my init of ScrolledText. 编辑:我添加了一个self.master = masterinit ,然后使用self.master而不是我自己的ScrolledText初始化中的self I am not sure why this fixed the problem and it would be nice to know why this fixed it. 我不知道为什么这可以解决问题,很高兴知道为什么可以解决这个问题。

This revision, with the class statement added, the Application frame gridded into the root window, and the text box not disabled, works. 此修订版(添加了class语句),“应用程序”框架网格化到了根窗口中并且未禁用文本框,它可以正常工作。

import tkinter as tk
import tkinter.scrolledtext as tkst
import tkinter.font as tkFont
import tkinter.ttk as ttk

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid()
        self.createWidgets()
    def createWidgets(self):
        self.logArea = tkst.ScrolledText(self,
                    wrap   = tk.WORD,
                    width  = 20,
                    height = 8,
                    #state="disabled",
                    name="logArea"
                )
        self.logArea.grid(padx=10, pady=10,row=1, sticky=tk.W)
        self.logArea.insert('1.0', 'test insertion')

root = tk.Tk()
app = Application(root)
app.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM