Python Tkinter:从纯网格迁移到画布结构中的框架

Python Tkinter:从纯网格迁移到画布结构中的框架,python,python-3.x,tkinter,tkinter-canvas,Python,Python 3.x,Tkinter,Tkinter Canvas,到目前为止,我的代码使用纯网格结构来放置按钮和文本,但这不允许我放置垂直滚动条。我需要改变我的代码,把它放在一个有框架的画布上,然后框架需要按钮+文本,画布需要有一个垂直滚动条 代码如下: import tkinter as tk from pixelregex import regex class App(tk.Tk): def __init__(self): tk.Tk.__init__(self) self.num_snippets = 1

到目前为止,我的代码使用纯网格结构来放置按钮和文本,但这不允许我放置垂直滚动条。我需要改变我的代码,把它放在一个有框架的画布上,然后框架需要按钮+文本,画布需要有一个垂直滚动条

代码如下:

import tkinter as tk
from pixelregex import regex

class App(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)

        self.num_snippets = 1

        self.initialize()

    def initialize(self):

        self.grid()
        self.entryVariable = tk.StringVar()

        # Just some buttons on the window
        button_start = tk.Button(self, text=u"Start", command=self.onStart)
        button_start.grid(row=0, column=0, sticky="EW")

        button_reset = tk.Button(self, text=u"Quit", command=self.onReset)
        button_reset.grid(row=0, column=1, sticky="EW")

        self.entry = tk.Entry(self, textvariable=self.entryVariable)
        self.entry.grid(row=0, column=2, sticky="EW")

        self.columnconfigure(1, weight=1)

    def onStart(self):

        # Get the value entered in the Entry widget
        dim = self.entryVariable.get()
        try:
            dim = int(dim)
        except ValueError:
            dim = 2  # Default
            print("Non integral dimension as input. Setting it to two ..")

        # FUnction that returns a list having 4 strings
        reg = regex.getRegex(dim)

        row_num = str(self.num_snippets)
        snippet_id = tk.Label(self, text=row_num, anchor="w", fg="blue", bg="white")
        snippet_id.grid(column=0, row=self.num_snippets, columnspan=1, sticky='EW')

        text = tk.Text(self, width=100, height=10)
        text.grid(row=self.num_snippets, column=1, columnspan=2, sticky="EW")

        input_string = "Top: " + reg[0] + "\n\n" + "Bottom: " + reg[1] + "\n\n" + "Left: " + reg[
            2] + "\n\n" + "Right: " + reg[3] + "\n\n"
        text.insert('1.0', input_string)

        self.num_snippets += 1

    def onReset(self):
        self.destroy()
        pass
我尝试过制作画布,在其中放置一个框架,使用grid()使用该框架插入按钮,但画布仍然是空的!我还需要插入一个垂直滚动条

import tkinter as tk

class App(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        self.btnframe = tk.Frame(self)
        self.btnframe.grid(row = 0, column = 0)
        self.textframe = tk.Frame(self)
        self.textframe.grid(row = 0, column = 1, sticky = "nsew")
        self.textframe.columnconfigure(0, weight=1)
        self.textframe.columnconfigure(1, weight=0)
        self.textframe.rowconfigure(0, weight=1)
        self.columnconfigure(1, weight=1)
        self.rowconfigure(0, weight=1)

        button_start = tk.Button(self.btnframe, text=u"Start", command=self.onStart)
        button_start.grid(row=0, column=0, sticky="n")
        button_reset = tk.Button(self.btnframe, text=u"Quit", command=self.onReset)
        button_reset.grid(row=0, column=1, sticky="n")

        self.text = tk.Text(self.textframe, width = 10, height = 10)
        self.text.grid(row=0, column=0, sticky="nsew")

        self.v_scroll_bar = tk.Scrollbar(self.textframe)
        self.v_scroll_bar.grid(row = 0, column = 1, sticky = 'nse')
        self.v_scroll_bar.config(command = self.text.yview)


    def onStart(self):
        for num in range(100):
            self.text.insert("end","{}{}".format(num, "\n"))
            self.text.see("end")


    def onReset(self):
        self.destroy()
        pass

if __name__ == "__main__":
    MyApp = App()
    tk.mainloop()

非常感谢您的帮助。

您无需为此使用画布。您可以只使用一个框架和滚动条

import tkinter as tk

class App(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        self.btnframe = tk.Frame(self)
        self.btnframe.grid(row = 0, column = 0)
        self.textframe = tk.Frame(self)
        self.textframe.grid(row = 0, column = 1, sticky = "nsew")
        self.textframe.columnconfigure(0, weight=1)
        self.textframe.columnconfigure(1, weight=0)
        self.textframe.rowconfigure(0, weight=1)
        self.columnconfigure(1, weight=1)
        self.rowconfigure(0, weight=1)

        button_start = tk.Button(self.btnframe, text=u"Start", command=self.onStart)
        button_start.grid(row=0, column=0, sticky="n")
        button_reset = tk.Button(self.btnframe, text=u"Quit", command=self.onReset)
        button_reset.grid(row=0, column=1, sticky="n")

        self.text = tk.Text(self.textframe, width = 10, height = 10)
        self.text.grid(row=0, column=0, sticky="nsew")

        self.v_scroll_bar = tk.Scrollbar(self.textframe)
        self.v_scroll_bar.grid(row = 0, column = 1, sticky = 'nse')
        self.v_scroll_bar.config(command = self.text.yview)


    def onStart(self):
        for num in range(100):
            self.text.insert("end","{}{}".format(num, "\n"))
            self.text.see("end")


    def onReset(self):
        self.destroy()
        pass

if __name__ == "__main__":
    MyApp = App()
    tk.mainloop()
下面是在文本框上使用垂直滚动条的示例

据我所知,您唯一需要使用带有滚动条的画布的时候是,如果您有多个帧或多个小部件要同时滚动

下面是如何设置Y轴滚动条

import tkinter as tk

class App(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        self.btnframe = tk.Frame(self)
        self.btnframe.grid(row = 0, column = 0)
        self.textframe = tk.Frame(self)
        self.textframe.grid(row = 0, column = 1, sticky = "nsew")
        self.textframe.columnconfigure(0, weight=1)
        self.textframe.columnconfigure(1, weight=0)
        self.textframe.rowconfigure(0, weight=1)
        self.columnconfigure(1, weight=1)
        self.rowconfigure(0, weight=1)

        button_start = tk.Button(self.btnframe, text=u"Start", command=self.onStart)
        button_start.grid(row=0, column=0, sticky="n")
        button_reset = tk.Button(self.btnframe, text=u"Quit", command=self.onReset)
        button_reset.grid(row=0, column=1, sticky="n")

        self.text = tk.Text(self.textframe, width = 10, height = 10)
        self.text.grid(row=0, column=0, sticky="nsew")

        self.v_scroll_bar = tk.Scrollbar(self.textframe)
        self.v_scroll_bar.grid(row = 0, column = 1, sticky = 'nse')
        self.v_scroll_bar.config(command = self.text.yview)


    def onStart(self):
        for num in range(100):
            self.text.insert("end","{}{}".format(num, "\n"))
            self.text.see("end")


    def onReset(self):
        self.destroy()
        pass

if __name__ == "__main__":
    MyApp = App()
    tk.mainloop()

如文所述,这个问题太宽泛了。听起来你是在要求我们为你写代码。我在画布中没有看到任何与框架相关的代码。你做了什么研究!你做了什么来尝试自己解决这个问题?你在这里搜索过可滚动框架的例子吗?你可以用tkinter放置一个垂直滚动条而不会出现问题。你需要滚动哪些信息?您是否只需要为
文本
小部件提供一个滚动条?