为单个窗口中显示的框架放置单个滚动条

时间:2019-04-11 07:53:12

标签: python python-3.x tkinter tkinter-canvas

必须在一个窗口中显示两个框架。第二个超过显示大小,并且滚动条必须放置在整个窗口中。

from tkinter import *

rectangle_values = []
k = 0

def open_window(rectangles):
    global k
    window.title('Rectangles')
    print(rectangles)
    for i in range(0, int(rectangles) * 2):
        if (i % 2) == 0:
            l4 = Label(frame2, text="Size of rectangle:")
            l4.grid(row=i, column=0)
        en = Entry(frame2)
        en.grid(row=i, column=1)

    frame2.tkraise()

    b4 = Button(frame2, text="Back", width=12, command=lambda: (frame1.tkraise(), window.title('Rectangle Configuration')))
    b4.grid(row=int(rectangles) * 2 + 1, column=1)
    k = int(rectangles)

def myfunction(event):
    canvas.configure(scrollregion=canvas.bbox("all"),width=220,height=700)
def _on_mousewheel(event):
    canvas.yview_scroll(-1*(event.delta/120), "units")   


window = Tk()
window.geometry('800x800')
frame1 = Frame(window)
frame1.grid(row = 2, column = 0)
frame2 = Frame(window)
frame2.grid(row = 10, column = 0)
frame1.tkraise()

l3 = Label(frame1, text="Number of Rectangles:")
l3.grid(row=0, column=0)

# defining entries
rectangles = StringVar()
e3 = Entry(frame1, textvariable=rectangles)
e3.grid(row=0, column=1)

# Defining buttons
b1 = Button(frame1, text='Submit', width=12, command=lambda: open_window(rectangles.get()))
b1.grid(row=3, column=1)

window.title("Rectangle Configuration")
window.mainloop()

所以,这是这里的代码。当矩形数大于18时,我们需要一个滚动条。

myframe=Frame(window)
canvas=Canvas(myframe)
frame=Frame(canvas)
myscrollbar=Scrollbar(myframe,orient="vertical",command=canvas.yview)
canvas.configure(yscrollcommand=myscrollbar.set)
canvas.bind_all('<MouseWheel>', lambda event: canvas.yview_scroll(int(-1*(event.delta/120)), "units"))
myscrollbar.pack(side="right",fill="y")
canvas.pack(side="left")
canvas.create_window((0,0),window=frame,anchor='nw')
frame.bind("<Configure>",myfunction)
window.bind("<MouseWheel>", myfunction)
myframe.grid(row=3, column=7)

我添加了这个,但是滚动条在两个框架之间。如何放置整个窗口的滚动条?

1 个答案:

答案 0 :(得分:1)

我认为您应该使用另一种方法,尝试使用更多的Canvas小部件,这样做会更容易。

import tkinter as tk


def open_window(rect):
    window.title('Rectangles')
    print(rect)
    x, y = 100, 50
    for i in range(0, rect * 2):
        if (i % 2) == 0:
            l4 = tk.Label(canvas, text="Size of rectangle:")
            canvas.create_window(0, y, window=l4, anchor=tk.NW)
        en = tk.Entry(canvas)
        canvas.create_window(x, y, window=en, anchor=tk.NW)
        y += 20
    canvas.configure(scrollregion=canvas.bbox("all"))


window = tk.Tk()
window.configure()

ws = window.winfo_screenwidth()
hs = window.winfo_screenheight()
w = 500  # width for the Tk root
h = 300  # height for the Tk root
x = (ws / 2) - (w / 2)
y = (hs / 2) - (h / 2)

window.geometry('%dx%d+%d+%d' % (w, h, x, y))
canvas = tk.Canvas(window, bg="black", width=500, height=300)
canvas.pack()
canvas_scroll = tk.Scrollbar(canvas, command=canvas.yview)
canvas_scroll.place(relx=1, rely=0, relheight=1, anchor=tk.NE)
canvas.configure(yscrollcommand=canvas_scroll.set, scrollregion=(0, 0, 500, 300))

rect_var = tk.IntVar()
entry_rect_number = tk.Entry(canvas, textvariable=rect_var)
canvas.create_window(0, 0, window=entry_rect_number, anchor=tk.NW)

sumbit_button = tk.Button(canvas, text="sumbit", command=lambda: open_window(rect_var.get()))
canvas.create_window(0, 20, window=sumbit_button, anchor=tk.NW)

# Here is the frame
canvas_frame1 = tk.Canvas(canvas, bg="red", width=150, height=290)
frame_scroll = tk.Scrollbar(canvas_frame1, command=canvas_frame1.yview)
frame_scroll.place(relx=1, rely=0, relheight=1, anchor=tk.NE)
canvas_frame1.configure(yscrollcommand=frame_scroll.set, scrollregion=(0, 0, 200, 400))

canvas_frame1.create_oval(0, 0, 50, 50)

canvas.create_window(250, 0, window=canvas_frame1, anchor=tk.NW)

canvas.bind_all('<MouseWheel>', lambda event: canvas.yview_scroll(int(-1*(event.delta/120)), "units"))

window.mainloop()

我不知道是否可以从Frame滚动main window,但是我的建议是创建一个Canvas,将所有widgets放到这里并将scrollbar添加到Canvas中,以便轻松滚动widgets内部的所有Canvas

有关Canvas及其所有方法的更多信息,请参见this site