Synchnorized Jenkins ssm magento jsf video awk rspec transactions msbuild Browserify 后台管理模板下载 郑州网站开发 js获取焦点事件 软件测试实战项目 pytorch安装教程 mysql批量更新数据 less的比较级 java高级特性 网页设计公司 windows搭建python开发环境 java最新框架 java如何连接mysql java运行环境 java原始数据类型 java新建文件 customerrors 火牛软件 马赛克软件 小米5c拆机 lseek函数 数科阅读器 php取整函数 千千静听老版本 maya2016教程 联发科mt6750 刷新页面 流水账软件 瑞兹技能 相册制作软件
当前位置: 首页 > 学习教程  > python

python tkinter 自适配屏幕尺寸动态显示窗口滚动条的Demo

2021/2/6 22:22:28 文章标签: 测试文章如有侵权请发送至邮箱809451989@qq.com投诉后文章立即删除

from tkinter import ttk, Tk, Frame, Canvas, Scrollbar, Buttondef paint_frame(frame): # 绘制窗口中的内容test_button Button(frame, text测试)test_button.grid(row0, column0, columnspan2, padx0, pady10)table ttk.Treeview(frame, height45, showheadings)table[c…

from tkinter import ttk, Tk, Frame, Canvas, Scrollbar, Button


def paint_frame(frame):  # 绘制窗口中的内容
    test_button = Button(frame, text='测试')
    test_button.grid(row=0, column=0, columnspan=2, padx=0, pady=10)
    table = ttk.Treeview(frame, height=45, show='headings')
    table['columns'] = tuple([str(i + 1) for i in range(15)])
    for column in table['columns']:
        table.column(column, width=100)
        table.heading(column, text=column)
    for index, goods_data in enumerate([[str(i + 1) for i in range(15)]] * 100):
        goods_data = [str(index + 1) + ':' + i for i in goods_data]
        table.insert('', 'end', values=goods_data)
    table.grid(row=1, column=0, padx=0, pady=0)
    frame.test_button = test_button
    frame.table = table

    # 为表格增加滚动条
    vbar = Scrollbar(frame, cursor='hand1', orient='vertical', command=table.yview)
    table.configure(yscrollcommand=vbar.set)
    vbar.grid(row=1, column=1, padx=0, pady=0, sticky='NS')
    frame.update()  # 刷新后才能获取到组件加载后的总内容尺寸


def get_size(screen_width, screen_height, frame_width, frame_height, scrollbar_zise=15):
    # canvas_width: 画布宽度, canvas_height: 画布高度
    # scroll_area_width: 滚动区域宽度, scroll_area_height: 滚动区域高度
    if screen_width >= frame_width and screen_height >= frame_height:  # 没有滚动条
        canvas_width = frame_width
        canvas_height = frame_height
        scroll_area_width = frame_width
        scroll_area_height = frame_height
    elif screen_width >= frame_width and screen_height < frame_height:  # 只有垂直滚动条
        canvas_width = frame_width + scrollbar_zise
        canvas_height = screen_height
        scroll_area_width = frame_width + scrollbar_zise  # 增加垂直滚动条占用的高度
        scroll_area_height = frame_height
    elif screen_width < frame_width and screen_height >= frame_height:  # 只有水平滚动条
        canvas_width = screen_width
        canvas_height = frame_height + scrollbar_zise
        scroll_area_width = frame_width
        scroll_area_height = frame_height + scrollbar_zise  # 增加水平滚动条占用的高度
    elif screen_width < frame_width and screen_height < frame_height:  # 同时有水平和垂直滚动条
        canvas_width = screen_width
        canvas_height = screen_height
        scroll_area_width = frame_width + scrollbar_zise  # 增加垂直滚动条占用的高度
        scroll_area_height = frame_height + scrollbar_zise  # 增加水平滚动条占用的高度
    return canvas_width, canvas_height, scroll_area_width, scroll_area_height


def paint_canvas(root, scrollbar_zise=15):
    # 生成画布,把frame绘制到画布上
    canvas = Canvas(root)
    frame = Frame(canvas)
    paint_frame(frame)  # 这里自定义绘制到窗口中的内容

    screen_width, screen_height = frame.winfo_screenwidth(), frame.winfo_screenheight()
    # 测试参数:
    # screen_width, screen_height = 1515, 920
    # screen_width, screen_height = 1000, 920
    # screen_width, screen_height = 1515, 600
    # screen_width, screen_height = 1000, 600
    # screen_width, screen_height = 1920, 600
    # screen_width, screen_height = 1000, 1080

    frame_width, frame_height = frame.winfo_reqwidth(), frame.winfo_reqheight()
    canvas_width, canvas_height, scroll_area_width, scroll_area_height = get_size(screen_width, screen_height, frame_width, frame_height, scrollbar_zise)

    # 垂直滚动条
    if scroll_area_height > canvas_height:
        vbar = Scrollbar(canvas, cursor='hand1', orient='vertical', command=canvas.yview)
        vbar.place(x=canvas_width - scrollbar_zise, width=scrollbar_zise, height=canvas_height)
        canvas.config(yscrollcommand=vbar.set)  # 绑定画布和滚动条的滚动

    # 水平滚动条
    if scroll_area_width > canvas_width:
        # 如果有垂直滚动条,水平滚动条宽度要减去垂直滚动条宽度
        hbar_width = canvas_width - (scroll_area_height > canvas_height and scrollbar_zise or 0)
        hbar = Scrollbar(canvas, cursor='hand1', orient='horizontal', command=canvas.xview)
        hbar.place(y=canvas_height - scrollbar_zise, width=hbar_width, height=scrollbar_zise)
        canvas.config(xscrollcommand=hbar.set)  # 绑定画布和滚动条的滚动

    # 刷新画布尺寸参数
    canvas.config(width=canvas_width, height=canvas_height, scrollregion=(0, 0, scroll_area_width, scroll_area_height))

    # 创建画布可见内容窗口,注意尺寸为frame尺寸的一半
    canvas.create_window((int(frame_width / 2), int(frame_height / 2)), window=frame)
    canvas.grid(row=0, column=0, padx=0, pady=0)

    # 返回窗口在屏幕中的位置
    return int((screen_width - canvas_width) / 2), int((screen_height - canvas_height - 100) / 2)


def paint_root(title):
    # scrollbar_zise为滚动条的高度或者宽度
    root = Tk()
    root.title(title)
    root.resizable(False, False)  # 禁用窗口缩放
    root.update_idletasks()

    paint_x, paint_y = paint_canvas(root)  # 在root上绘制画布

    # 调整窗口位置
    root.geometry('+%s+%s' % (paint_x, paint_y))
    root.deiconify()
    root.mainloop()

if __name__ == '__main__':
    paint_root('主界面')

 


本文链接: http://www.dtmao.cc/news_show_2000033.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?