gpt4 book ai didi

python - tkinter - Canvas 中的滚动条不起作用

转载 作者:行者123 更新时间:2023-11-30 23:00:50 24 4
gpt4 key购买 nike

我已经搜索了堆栈溢出,但大多数解决方案似乎对我不起作用,或者答案不够清晰,我不知道应该在哪里/如何使用代码。由于某种原因,我的 AddSubject 框架中的水平滚动条无法正常工作。如何让水平滚动条工作?

AddSubjects Frame

这是我的代码:

import tkinter as tk
from tkinter import ttk
import tkinter.scrolledtext as tks

class Program(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)

tk.Tk.iconbitmap(self, default = "")
tk.Tk.wm_title(self, "")

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 (SubjectHome, AddSubject):

frame = F(container, self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")

self.show_frame(SubjectHome)



def show_frame(self,cont):
frame = self.frames[cont]
frame.tkraise()


class SubjectHome(tk.Frame):

def __init__(self, parent, controller):

tk.Frame.__init__(self, parent)

ttk.Style().configure("TButton", padding=6, relief="flat", background="#ccc")

name = tk.Label(self, text = "User: FirstName + LastName")
name.pack(anchor="ne")

pagename = tk.Label(self, text = "Subject Menu")
pagename.pack(anchor="n")

self.innerFrame = tk.Frame(self, bg="red")
self.innerFrame.place(relx=.5, rely=.5, anchor="c")


view = ttk.Button(self.innerFrame, text = "View Subjects", command = lambda: controller.show_frame(ViewSubject))
view.grid(row=0, sticky="W"+"E")

add = ttk.Button(self.innerFrame, text = "Add Subjects", command = lambda: controller.show_frame(AddSubject))
add.grid(row=1, sticky="W"+"E")


class AddSubject(tk.Frame):

def __init__(self, parent, controller):

tk.Frame.__init__(self, parent)

name = tk.Label(self, text = "User: FirstName + LastName")
name.pack(anchor="ne")

pagename = tk.Label(self, text = "Add Subjects")
pagename.pack(anchor="n")

self.innerFrame = tk.Frame(self)
self.innerFrame.place(relx=.5, rely=.5, anchor="c")

canvas = tk.Canvas(self.innerFrame)

self.firstFrame = tk.Frame(canvas)
self.firstFrame.pack(anchor="n")

info = tk.Label(self.innerFrame, text = "Information...\n Information....")
info.pack()

for x in range(5):
pagename = tk.Label(self.firstFrame, text = "Unit Name")
pagename.grid(row=0, column=x)
self.text = tks.ScrolledText(self.firstFrame, width=50)
self.text.grid(row=1, column=x ,sticky="E")

scrollbar = tk.Scrollbar(self.innerFrame, orient="horizontal", command=canvas.xview)
canvas.configure(xscrollcommand=scrollbar.set)
scrollbar.pack(side="bottom", fill="x")
canvas.pack(side="left", fill="both", expand=True)

back = ttk.Button(self.innerFrame, text = "Back", command = lambda: controller.show_frame(SubjectHome))
back.pack(anchor="sw")

next = ttk.Button(self.innerFrame, text = "Next")
next.pack(anchor="se")


app = Program()
app.state('zoomed')
app.mainloop()

最佳答案

要使小部件在 Canvas 上可滚动,必须使用 create_window 将其添加到 Canvas 中。如果您调用 packgridplace 将小部件放入 Canvas 中,它们将不会滚动。

关于python - tkinter - Canvas 中的滚动条不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35141856/

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