gpt4 book ai didi

python - 为什么 Canvas 在添加小部件时不随滚动条滚动并自动调整大小?

转载 作者:太空宇宙 更新时间:2023-11-03 17:58:46 26 4
gpt4 key购买 nike

当我将小部件放入 Canvas 中时,小部件会绕过 Canvas 大小。但是当我滚动时, Canvas 不会移动。
最重要的是,当我添加更多小部件时, Canvas 会自动调整大小。我认为 Canvas 不应该重新调整大小,因为我已经添加了滚动条?

这是我的代码:

from tkinter import *
from tkinter import ttk
import mysql.connector

class CanvasScroll:

def on_resize(self,event):
self.canvas.config(width = 1185, height = 530)

def add_crew(self):
crewFrame = CrewFrame()


def callback_list(self):
index = self.notebook.index(self.notebook.select()) + 1
self.tmpframe = Try2(self.contentframe, index)


def __init__(self, master):
self.canvas = Canvas(master, width = 1185, height = 530, scrollregion = (0, 0, 1216, 700), bg = 'white', confine = True)
self.canvas.grid(row = 2, column = 0, sticky = 'news')

self.xscroll = ttk.Scrollbar(master, orient = HORIZONTAL, command = self.canvas.xview)
self.xscroll.grid(row = 3, column = 0, sticky = 'we')

self.yscroll = ttk.Scrollbar(master, orient = VERTICAL, command = self.canvas.yview)
self.yscroll.grid(row = 2, column = 1, sticky = 'ns')

self.canvas.config(xscrollcommand = self.xscroll.set, yscrollcommand = self.yscroll.set)
self.canvas.bind("<Configure>", self.on_resize)


self.option = ttk.Frame(master, height = 150, width = 1206)
self.option.grid(row = 0, column = 0, sticky = 'we', columnspan = 5)

self.addicon = PhotoImage(file = 'C:\\Users\\rain\\Desktop\\Fyosh!\\logo\\add.gif').subsample(2,2)
self.btnAdd = ttk.Button(self.option, image = self.addicon, text = 'Add Crew', compound = TOP, command = self.add_crew)
self.btnAdd.grid(row = 0, column = 0, padx = 50, pady = 5)

self.updateicon = PhotoImage(file = 'C:\\Users\\rain\\Desktop\\Fyosh!\\logo\\update.gif').subsample(2,2)
self.btnUpdate = ttk.Button(self.option, image = self.updateicon, text = 'Update Crew', compound = TOP)
self.btnUpdate.grid(row = 0, column = 1, padx = 50)

self.deleteicon = PhotoImage(file = 'C:\\Users\\rain\\Desktop\\Fyosh!\\logo\\delete.gif').subsample(2,2)
self.btnDelete = ttk.Button(self.option, image = self.deleteicon, text = 'Delete Crew', compound = TOP)
self.btnDelete.grid(row = 0, column = 2, padx = 50)

self.reloadicon = PhotoImage(file = 'C:\\Users\\rain\\Desktop\\Fyosh!\\logo\\Refresh.png').subsample(7,7)
self.reloadbtn = ttk.Button(self.option, image = self.reloadicon, command = self.callback_list, text = 'Load List', compound = TOP)
self.reloadbtn.grid(row = 0, column = 3, padx = 50)

self.tabframe = ttk.Frame(master, height = 20)
self.tabframe.grid(row = 1, sticky = 'we')

self.notebook = ttk.Notebook(self.tabframe)
self.notebook.grid(row = 0, column = 0, sticky = 'we')

db = mysql.connector.connect(user = 'root', password = '', host = 'localhost', database = 'fat2x_payroll')
cursor = db.cursor()

ships = ("SELECT * from tbl_shiplist")

try:
cursor.execute(ships)
result = cursor.fetchall()
self.tab = {}
for row in result:
self.tab[row[0]] = {
"shipname": ttk.Frame(self.notebook)
}
self.notebook.add(self.tab[row[0]]["shipname"], text = row[1])
db.close()

except:
messagebox.showinfo("alert", "ship list error")

self.canvas.label = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 0)
self.canvas.label1 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 1)
self.canvas.label2 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 2)
self.canvas.label3 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 3)
self.canvas.label4 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 4)
self.canvas.label5 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 5)
self.canvas.label6 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 6)
self.canvas.label7 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 7)
self.canvas.label8 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 8)

def start():
master = Tk()
master.geometry("1206x690+10+10")
master.resizable(0,0)
master.title("Crew Panel")
canvasScroll = CanvasScroll(master)

if __name__ == "__main__":
start()

最佳答案

Canvas 只能滚动嵌入在 Canvas 上的对象。对于小部件,这意味着您已调用 create_window您希望受滚动条影响的小部件的 Canvas 方法。

如果你想使用grid来组织小部件,并且希望这些小部件位于可滚动的容器中,通常的方法是将这些小部件放在一个框架中,然后将框架添加到带有 create_window 的 Canvas 。您可以在此答案中看到此技术的示例:https://stackoverflow.com/a/3092341/7432

至于自动调整大小,此行为与使用滚动条完全无关。如果您在小部件上使用gridpack,则父级将始终调整大小,除非您关闭了几何传播。无论它是 Canvas、Frame、Toplevel 还是任何其他类别的小部件,都没有关系。

关于python - 为什么 Canvas 在添加小部件时不随滚动条滚动并自动调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28048140/

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