gpt4 book ai didi

python - Tkinter ;如何在主根窗口中按下按钮时打开的弹出/顶级窗口中使用滚动条

转载 作者:行者123 更新时间:2023-11-28 22:50:27 24 4
gpt4 key购买 nike

我正在尝试在顶层窗口中创建一个带有滚动条的应用程序,当在主根窗口中按下按钮 bttn 时,该窗口会打开,根据下面的代码,我需要以下方面的帮助:

  • 通过按下 widget() 中定义的按钮 bttn,滚动条应该位于主根窗口的新顶层窗口中
  • 新的弹出窗口应该包含VerticalScrollFrame中定义的滚动条vscrollbar
  • 滚动条vscrollbar不应该出现在主根窗口中,只能出现在新的弹出窗口中

    from Tkinter import * 
    class VerticalScrolledFrame(Frame):


    def __init__(self, parent, *args, **kw):
    Frame.__init__(self, parent, *args, **kw)

    # create a canvas object and a vertical scrollbar for scrolling it
    vscrollbar = Scrollbar(self, orient=VERTICAL)
    vscrollbar.pack(fill=Y, side=RIGHT, expand=FALSE)
    canvas = Canvas(self, bd=0, highlightthickness=0,
    yscrollcommand=vscrollbar.set)
    canvas.pack(side=LEFT, fill=BOTH, expand=TRUE)
    vscrollbar.config(command=canvas.yview)

    # reset the view
    canvas.xview_moveto(0)
    canvas.yview_moveto(0)

    # create a frame inside the canvas which will be scrolled with it
    self.interior = interior = Frame(canvas)
    interior_id = canvas.create_window(0, 0, window=interior,
    anchor=NW)

    # track changes to the canvas and frame width and sync them,
    # also updating the scrollbar
    def _configure_interior(event):
    # update the scrollbars to match the size of the inner frame
    size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
    canvas.config(scrollregion="0 0 %s %s" % size)
    if interior.winfo_reqwidth() != canvas.winfo_width():
    # update the canvas's width to fit the inner frame
    canvas.config(width=interior.winfo_reqwidth())
    interior.bind('<Configure>', _configure_interior)

    def _configure_canvas(event):
    if interior.winfo_reqwidth() != canvas.winfo_width():
    # update the inner frame's width to fill the canvas
    canvas.itemconfigure(interior_id, width=canvas.winfo_width())
    canvas.bind('<Configure>', _configure_canvas)


    if __name__ == "__main__":

    class SampleApp(Frame):
    def __init__(self,root, *args, **kwargs):
    Frame.__init__(self, root,*args, **kwargs)


    self.frame = VerticalScrolledFrame(root)
    self.frame.pack()

    self.widget()


    def widget(self):
    self.label = Label(text="Shrink the window to activate the scrollbar.")
    self.label.pack()

    bttn = Button(self.frame.interior, text = "Flytta fram/bak i listvy", command = self.open_new_window_with_text_and_scrollbar)
    bttn.pack()

    # Should be opened in a new, top-level, window from the main root window by pressing the #button bttn defined in widget()
    # the new popup window should contain the scrollbar vscrollbar defined in SampleApp
    # the scrollbar vscrollbar shouldn't appear in the main root window, only in the new #popup window

    def open_new_window_with_text_and_scrollbar(self):

    self.top = tk.Toplevel(self)
    frame = VerticalScrolledFrame(root)
    frame.pack()
    Button = tk.Button(self.top, text="Close window", command=self.top.destroy)
    Label = tk.Label(self.top, wraplength = 500,text="testing")
    Label.pack(side="left", fill="both", expand=True)
    Button.pack()



    root = tk.Tk()
    root.title("Maltparser1.0_demo")

    root.geometry("900x700")
    app = SampleApp(root)
    root.mainloop()

最佳答案

您正在将 root 作为父级传递给 VerticalScrolledFrame,您需要改为传递 self.top:

class SampleApp(Frame):

# ...Other code...

def open_new_window_with_text_and_scrollbar(self):
self.top = tk.Toplevel(self)
frame = VerticalScrolledFrame(self.top)
frame.pack()
# Other widgets

这就是为什么它将自己添加到主根窗口而不是 Toplevel

关于python - Tkinter ;如何在主根窗口中按下按钮时打开的弹出/顶级窗口中使用滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22645041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com