找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 1884|回复: 1

[求助] 关于tkinter滚动条显示其他组件问题

1

主题

2

帖子

2

积分

贫民

积分
2
jinhaiyu 发表于 2020-10-19 17:15:07 | 显示全部楼层 |阅读模式


想做一个自动化程序
预期效果:首页只显示10行,可根据滚动条滑动浏览下面的输入框
每一行(下拉框+输入框+加号按钮+减号按钮)都是一个frame
显示的又是一个frame
百度上说先设置一个Scrollbar,在把frame放进去,在给Scrollbar设置滚动条,但没有实现效果
请问有什么办法吗?


下载.png
回复

使用道具 举报

1

主题

2

帖子

2

积分

贫民

积分
2
jinhaiyu  楼主| 发表于 2020-10-21 15:03:53 | 显示全部楼层
本帖最后由 jinhaiyu 于 2020-10-21 15:12 编辑

#源代码

import tkinter as tk
from tkinter import Listbox, ttk,Canvas,Scrollbar


class Page_Table():
    def __init__(self):
        self.window = tk.Tk()
        self.window.title("自动化程序")
        # 获取电脑屏幕大小
        screen_width, screen_height = self.window.maxsize()
        # 设置窗口大小,让其在电脑屏幕中央显示
        self.window.geometry("1000x800+{}+{}".format(int((screen_width - 1000) / 2), int((screen_height - 800) / 2)))
        # 禁止调整尺寸
        self.window.resizable(0, 0)

        # 用例列表构件
        list_case_Frame_x = 0
        list_case_Frame_y = 60
        list_case_Frame_width = 300
        list_case_Frame_height = 800 - 60
        list_case_Frame_Top_height = 40
        self.list_case_Frame = tk.Frame(self.window)
        self.list_case_Frame.place(x=list_case_Frame_x, y=list_case_Frame_y, width=list_case_Frame_width,
                                   height=list_case_Frame_height)
        self.list_case_Frame.config(background="LightGrey")

        # 目录->用例列表构件
        self.catalog_bt = tk.Button(self.list_case_Frame, text="目录", bg="LightGrey", bd=0, fg="DimGray",
                                    font=("helv36", "15"))
        self.catalog_bt.place(x=list_case_Frame_x, y=0, width=list_case_Frame_width / 2,
                              height=list_case_Frame_Top_height)

        # 用例->用例列表构件
        self.case_bt = tk.Button(self.list_case_Frame, text="用例", bg="LightGrey", bd=0, fg="DimGray",
                                 font=("helv36", "15"))
        self.case_bt.place(x=list_case_Frame_width / 2, y=0, width=list_case_Frame_width / 2,
                           height=list_case_Frame_Top_height)

        # 目录列表
        self.catalog_list = Listbox(self.list_case_Frame, bd=0, bg="LightGrey")
        self.catalog_list.place(x=list_case_Frame_x, y=list_case_Frame_Top_height, width=list_case_Frame_width / 2,
                                height=list_case_Frame_height - list_case_Frame_Top_height)
        #
        # # 用例列表
        self.case_list = Listbox(self.list_case_Frame, bd=0, bg="LightGrey")
        self.case_list.place(x=list_case_Frame_x + list_case_Frame_width / 2, y=list_case_Frame_Top_height,
                             width=list_case_Frame_width / 2,
                             height=list_case_Frame_height - list_case_Frame_Top_height)

        case_step_x = 300.5
        case_step_y = 60
        case_step_width = 700
        case_step_height = 500
        self.case_step_frame = tk.Frame(self.window)
        self.case_step_frame.place(x=case_step_x, y=case_step_y, width=case_step_width, height=case_step_height)
        self.case_step_frame.config(background="LightGrey")

        # 设置canvas,放置滚动条
        self.case_canvas = Canvas(self.case_step_frame)
        self.case_canvas.place(x=80, y=50, width=560, height=300)
        self.case_canvas.config(background="LightGrey")

        self.case_name_frame = tk.Frame(self.case_canvas)
        self.case_name_frame.place(x=0, y=0, width=560, height=400)
        self.case_name_frame.config(background="LightGrey")

        vbar = Scrollbar(self.case_name_frame)
        vbar.place(x=540, y=0, width=20, height=400)
        vbar.configure(command=self.case_canvas.yview)

        self.case_frame_y = 0
        #现存控件列表
        self.case_name = []
        #总控件列表
        self.case_name_all = []
        for i in range(10):
            j = 0

            self.case_frame = tk.Frame(self.case_name_frame)
            self.case_frame.place(x=0, y=self.case_frame_y, width=540, height=30)

            # 下拉框
            self.action_list = ttk.Combobox(self.case_frame, values=["click", "url", "close"])
            self.action_list.place(x=0, y=0, width=80,
                                   height=30)
            #输入框
            self.input_list = tk.Entry(self.case_frame, width=200, font=("helv36", "10"))
            self.input_list.place(x=80, y=0, width=400,
                                  height=30)
            # 加号
            self.insert_list = tk.Button(self.case_frame, text="+", bg="LightGrey", bd=0, fg="black",
                                         font=("helv36", "15"))
            self.insert_list.place(x=400+80, y=0, width=30,
                                   height=30)
            #减号
            self.delete_list = tk.Button(self.case_frame, text="-", bg="LightGrey", bd=0, fg="black",
                                         font=("helv36", "15"), command=lambda i=i,j=j: p.case_insert(i,j))

            self.delete_list.place(x=400 + 30+80, y=0,
                                   width=30,
                                   height=30)

            self.case_name.append(self.case_frame)
            self.case_name_all.append(self.case_frame)

            self.case_frame_y += 30

    def case_insert(self, num,var):
        print(num)
        try:
            if len(self.case_name) > 1:
                self.case_name_all[num].destroy()
                print("{}已销毁".format(num))
                self.case_name.remove(self.case_name_all[num])
                p.Control_Display()
                # print(self.case_name)
        except:
            pass

    def Control_Display(self):

        for i in range(len(self.case_name)):
            print(i)
            try:
                self.case_name.place_forget()
                print("{}已隐藏".format(i))
                self.case_name.place(x=0, y=(i)*30, width=540, height=30)
                print("{}已重新放置".format(i))
                for i in self.case_name:
                    print(str(i))
            except:
                pass


    def Run(self):
        self.window.mainloop()


if __name__ == '__main__':
    p = Page_Table()
    p.Run()
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表