我有一个在主窗口中打开的画布,但是我希望它在Frame内部的画布中打开。我试图添加一些疯狂的属性(因为它们在我的手中),但是它们并不能解决我的问题,因此我删除了它们,不知道该怎么办。您能帮我吗-我怎么能知道呢?
代码:
from tkinter import *
from tkinter import ttk
class Example(Frame): # Frame
def __init__(self, root):
Frame.__init__(self, root)
# ---- for grid ----
canvas_width = 1000 # MUTABLE!!!
canvas_height = 1000 # MUTABLE!!!
self.canvas_width = canvas_width
self.canvas_height = canvas_height
self.warehouse_canvas = Canvas(self, width=canvas_width, height=canvas_height, background="bisque")
self.x_scrollbar = Scrollbar(self, orient="horizontal", command=self.warehouse_canvas.xview)
self.y_scrollbar = Scrollbar(self, orient="vertical", command=self.warehouse_canvas.yview)
self.warehouse_canvas.configure(yscrollcommand=self.y_scrollbar.set, xscrollcommand=self.x_scrollbar.set)
self.warehouse_canvas.configure(scrollregion=(0, 0, canvas_width, canvas_height)) # c_w c_h изменены
self.x_scrollbar.grid(row=1, column=0, sticky="ew")
self.y_scrollbar.grid(row=0, column=1, sticky="ns")
self.warehouse_canvas.grid(row=0, column=0, sticky="nsew")
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
root.columnconfigure(1, weight=1)
root.rowconfigure(1, weight=1)
self.warehouse_canvas.bind("<ButtonPress-1>", self.scroll_start)
self.warehouse_canvas.bind("<B1-Motion>", self.scroll_move)
self.draw_grid(self.warehouse_canvas, canvas_width, canvas_height) # !!!
def draw_grid(root, warehouse_canvas, canvas_width, canvas_height):
for line in range(0, canvas_width, 10):
warehouse_canvas.create_line([(line, 0), (line, canvas_height)], fill='#d9d9d9', tags='grid_line_width')
for line in range(0, canvas_width, 10):
warehouse_canvas.create_line([(0, line), (canvas_width, line)], fill='#d9d9d9', tags='grid_line_height')
# warehouse_canvas.grid(row=0, column=0) color : #d9d9d9
def scroll_start(self, event):
self.warehouse_canvas.scan_mark(event.x, event.y)
def scroll_move(self, event):
self.warehouse_canvas.scan_dragto(event.x, event.y, gain=1)
if __name__ == "__main__":
root = Tk()
Example(root).grid(row=0, column=0, # положение
columnspan=2, rowspan=2, sticky="nsew")
root.mainloop()
有什么方法可以不用重写整个代码吗?