首先,我想感谢您的提前帮助,因为这个问题困扰了我很长时间。在互联网上搜索后,我仍然找不到适合该问题的答案。
想法是将框架作为基本窗口。然后在框架内创建一个画布以放置多个按钮,这就是滚动条的原因。
我设法使滚动条显示出来,但是它没有任何功能。 (代码的第三部分) 我试图查找类似的问题,但大多数情况下,小部件都是标签而不是按钮。
这是代码/图片及其外观。 非常感谢!!!!
import tkinter as tk
import tkinter.font as tkFont
from PIL import Image, ImageTk
def frontpage():
#1. window setup
root = tk.Tk()
root.title("Title")
screenWidth = root.winfo_screenwidth()
screenHeight = root.winfo_screenheight()
w = 1200
h = 680
x = (screenWidth - w) / 2
y = (screenHeight - h ) / 4
root.geometry("%dx%d+%d+%d" % (w,h,x,y))
#2. window background
image = Image.open("secondpage/sec.gif")
yellowstone = ImageTk.PhotoImage(image)
label = tk.Label(root,image=yellowstone)
label.pack()
#3.frame with canvas and scrollbar
canvas_frame = tk.Frame(root) #frame set up
canvas_frame.place(x=150,y=95)
canvas_frame_scrollbar = tk.Scrollbar(canvas_frame) #scrollbar set up
canvas_frame_scrollbar.pack(side=tk.RIGHT,fill=tk.Y)
canvas = tk.Canvas(canvas_frame, width=898, height=490,bd=0,bg='#f9ca7c') #canvas set up
canvas.pack()
#4.button design button picture size=100*100
person_name_font= tkFont.Font(size=5, weight="bold") #font design
front_p1_pic = tk.PhotoImage(file="secondpage/p1.gif") # button image
front_p1 = tk.Button(canvas_frame,image= front_p1_pic,compound=tk.TOP,font= person_name_font)
front_p2 = tk.Button(canvas_frame,image= front_p1_pic,compound=tk.TOP,font= person_name_font)
#4-1.button place
front_p1.place(x=56,y=30,width=100,height=100)
front_p2.place(x=232,y=30,width=100,height=100)
root.mainloop()
frontpage()
照片1是它的外观,照片2和3是背景和按钮的图像 [1]:https://i.stack.imgur.com/K6rLY.png [2]:https://i.stack.imgur.com/5M8su.gif [3]:https://i.stack.imgur.com/y0WXi.gif