Cannot use geometry manager pack inside

前端 未结 3 955
梦如初夏
梦如初夏 2020-11-27 18:56

So I\'m making an rss reader using the tkinter library, and in one of my methods I create a text widget. It displays fine until I try to add scrollbars to it.

Here i

相关标签:
3条回答
  • 2020-11-27 18:59

    Per the docs, don't mix pack and grid in the same master window:

    Warning: Never mix grid and pack in the same master window. Tkinter will happily spend the rest of your lifetime trying to negotiate a solution that both managers are happy with. Instead of waiting, kill the application, and take another look at your code. A common mistake is to use the wrong parent for some of the widgets.

    Thus, if you call grid on the textbox, do not call pack on the scrollbar.


    import Tkinter as tk
    import ttk
    
    class App(object):
        def __init__(self, master, **kwargs):
            self.master = master
            self.create_text()
    
        def create_text(self):
            self.textbox = tk.Text(self.master, height = 10, width = 79, wrap = 'word')
            vertscroll = ttk.Scrollbar(self.master)
            vertscroll.config(command=self.textbox.yview)
            self.textbox.config(yscrollcommand=vertscroll.set)
            self.textbox.grid(column=0, row=0)
            vertscroll.grid(column=1, row=0, sticky='NS')
    
    root = tk.Tk()
    app = App(root)
    root.mainloop()
    
    0 讨论(0)
  • 2020-11-27 19:17

    The reason of the code is simple, you CANNOT use pack and grid inside the same class or for the same frame. Thus, use only one.

    0 讨论(0)
  • 2020-11-27 19:20

    You can't use pack and grid inside the same class or the same frame.use only one

    0 讨论(0)
提交回复
热议问题