Tkinter - 只需要一个垂直滚动条 window - 多个 windows (框架)在一个容器内
Tkinter - need a vertical scrollbar in only one window - multiple windows (frames) are within a container
我正在 Python 3.7 中编写一个 tkinter 应用程序,但我以前没有使用过 GUI,而且我不是很有经验。我只想在我的三个 windows 中添加一个带有垂直滚动条的 canvas。页面通过按钮相互连接 - 当我按下 StartPage 上的按钮时,它会将我带到 PageOne,这样我就有了将我带到不同页面的按钮。每个页面都是主容器中的一个单独的 class,并且根据我单击的按钮(PageOne、PageTwo、PageThree),使用 show_frame 和 tkraise 调出该页面。由于所有页面都在一个容器中,我不知道如何在该容器中的一个页面上实现滚动条。我在这里 https://gist.github.com/bhaskar-nair2/94b2d4dd511a1cd38ecde9c9481c4b28 找到了一段有趣的滚动条代码,但我没有设法将它集成到我的代码中。我只需要在 PageOne window 中有一个滚动条。如果有任何帮助,我将不胜感激。
这是我的代码的一部分:
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import pandas as pd
import tkinter as tk
from tkinter import ttk
import matplotlib.pyplot as plt
import scipy as sp
import numpy as np
from matplotlib.ticker import NullFormatter
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
NORM_FONT = ("Helvetica", 10)
SMALL_FONT = ("Helvetica", 8)
class MyApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Data Visualisation App")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo, PageThree):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="Start Menu", font=NORM_FONT)
label.pack(pady=10,padx=10)
button1 = ttk.Button(self, text="Page 1",
command=lambda: controller.show_frame(PageOne))
button1.pack()
button2 = ttk.Button(self, text="Page 2",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
button3 = ttk.Button(self, text="Page 3",
command=lambda: controller.show_frame(PageThree))
button3.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button1 = ttk.Button(self, text="Back to Start Page",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = ttk.Button(self, text="Page 2",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button1 = ttk.Button(self, text="Back to Start Page",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = ttk.Button(self, text="Page 1",
command=lambda: controller.show_frame(PageOne))
button2.pack()
class PageThree(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button1 = ttk.Button(self, text="Back to Start Page",
command=lambda: controller.show_frame(StartPage))
button1.pack()
app = MyApp()
app.lift()
app.mainloop()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button1 = ttk.Button(self, text="Back to Start Page",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = ttk.Button(self, text="Page 2",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
self.frame = VerticalScrolledFrame(self) <--- here!!!
self.frame.pack() <--- here!!!
VerticalScrolledFrame
来自您链接的 repo,我只是将所有 orient
和 position
转为小写和字符串形式。
从概念上讲,按钮和滚动条的放置方式没有区别。如果你想在say PageOne
中放置一些东西,你将进入PageOne
的__init__
定义并将self
放入框架的定义中并打包。
如果你想要另一个观点,你有一个容器,你可以在其中放置 GUI 的页面,但这些页面是框架,因此可以用作其他对象(按钮、标签、滚动条、其他框架等)的容器。 .
我正在 Python 3.7 中编写一个 tkinter 应用程序,但我以前没有使用过 GUI,而且我不是很有经验。我只想在我的三个 windows 中添加一个带有垂直滚动条的 canvas。页面通过按钮相互连接 - 当我按下 StartPage 上的按钮时,它会将我带到 PageOne,这样我就有了将我带到不同页面的按钮。每个页面都是主容器中的一个单独的 class,并且根据我单击的按钮(PageOne、PageTwo、PageThree),使用 show_frame 和 tkraise 调出该页面。由于所有页面都在一个容器中,我不知道如何在该容器中的一个页面上实现滚动条。我在这里 https://gist.github.com/bhaskar-nair2/94b2d4dd511a1cd38ecde9c9481c4b28 找到了一段有趣的滚动条代码,但我没有设法将它集成到我的代码中。我只需要在 PageOne window 中有一个滚动条。如果有任何帮助,我将不胜感激。 这是我的代码的一部分:
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import pandas as pd
import tkinter as tk
from tkinter import ttk
import matplotlib.pyplot as plt
import scipy as sp
import numpy as np
from matplotlib.ticker import NullFormatter
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
NORM_FONT = ("Helvetica", 10)
SMALL_FONT = ("Helvetica", 8)
class MyApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Data Visualisation App")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo, PageThree):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="Start Menu", font=NORM_FONT)
label.pack(pady=10,padx=10)
button1 = ttk.Button(self, text="Page 1",
command=lambda: controller.show_frame(PageOne))
button1.pack()
button2 = ttk.Button(self, text="Page 2",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
button3 = ttk.Button(self, text="Page 3",
command=lambda: controller.show_frame(PageThree))
button3.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button1 = ttk.Button(self, text="Back to Start Page",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = ttk.Button(self, text="Page 2",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button1 = ttk.Button(self, text="Back to Start Page",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = ttk.Button(self, text="Page 1",
command=lambda: controller.show_frame(PageOne))
button2.pack()
class PageThree(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button1 = ttk.Button(self, text="Back to Start Page",
command=lambda: controller.show_frame(StartPage))
button1.pack()
app = MyApp()
app.lift()
app.mainloop()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button1 = ttk.Button(self, text="Back to Start Page",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = ttk.Button(self, text="Page 2",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
self.frame = VerticalScrolledFrame(self) <--- here!!!
self.frame.pack() <--- here!!!
VerticalScrolledFrame
来自您链接的 repo,我只是将所有 orient
和 position
转为小写和字符串形式。
从概念上讲,按钮和滚动条的放置方式没有区别。如果你想在say PageOne
中放置一些东西,你将进入PageOne
的__init__
定义并将self
放入框架的定义中并打包。
如果你想要另一个观点,你有一个容器,你可以在其中放置 GUI 的页面,但这些页面是框架,因此可以用作其他对象(按钮、标签、滚动条、其他框架等)的容器。 .