pythonGUI(三)容器

列元素 Column

一个容器元素,用于在窗口的布局中创建布局

sg.Column()orsg.Col()

Column(
	layout,
	# 将在“列”容器中显示的布局  [[sg.T('hello world')]]

    background_color=None,
    # 整栏背景色

    size=(None, None),
    # (宽度,高度)以像素为单位的尺寸(
    # 偶尔不工作。

    pad=None,
    # 与周围元素间的间隔设定 左右上下

    scrollable=False,
    # 如果为True,则滚动条将添加到该列

    vertical_scroll_only=False,
    # 如果为True,则不会显示水平滚动条

    right_click_menu=None,
    # 右击调出菜单

    key=None,
    # 元素的唯一标识符

    visible=True,
    # 元素的可见状态设定

    justification="left",
    # 为列本身设置对齐方式(水平方向)

    element_justification="left",
    # 列内所有元素的对齐方式 
    # 有效值为 left,center,right

    vertical_alignment=None,
    # 垂直对齐方式
    # 有效值为top,bottom,center

    grab=None,
    # 如果设定为True,可以拖拽此元素移动窗口

    expand_x=None,
    # 如果为True,则列将自动沿X方向扩展以填充可用空间

    expand_y=None,
    # 如果为True,则列将自动沿y方向扩展以填充可用空间

		)

Update方法

window[key].Update(visible=None)

import  PySimpleGUI as sg

col_layout = [[sg.T("姓名",background_color="blue")],[sg.In(size=(10,1))],
              [sg.T("性别",background_color="blue")],[sg.In(size=(10,1))],
              ]

layout = [[sg.Col(col_layout,
                  background_color="blue",
                  size = (100,500),
                  # scrollable=True,
                  # vertical_scroll_only=True,
                  key="-Col-",
                  # element_justification="left",
                  justification='center',
                  # vertical_alignment="bottom",
                  )],[sg.LB(range(1,10),size=(100,100))]]

event, value = sg.Window("pthon GUI", layout, keep_on_top=True, resizable=True).read(close=True)

框架元素

容器元素,可以存放其他元素。
四周有线条围绕和一个文本元素作为标题

sg.Frame()


Frame(
    title,
    # 标题文本
    # str

    layout,
    # 容器内元素布局
    # [[sg.In()]]

    title_color=None,
    # 标题文本颜色设定

    background_color=None,
    # 框架元素背景颜色

    title_location=None,
    # 标题所处位置
    # 有效值有12种(ewsn 东西南北)
    # e,es,en,s,se,sw,w,ws,wn,n,ne,nw
    # 默认为nw
    relief="groove",
    # 浮雕设计  和其他元素的有效值是一样的。
    # raised,sunken,flat,groove,ridge,solid

    size=(None, None),
    # 元素的宽度和行高
    # 偶尔不起作用

    font=None,
    # 可以设置字体名称,大小等等

    pad=None,
    # 和周围元素间隔的设定((left, right), (top, bottom))

    border_width=None,
    # 框架元素的线条宽度

    key=None,
    # 框架元素的唯一标识符

    tooltip=None,
    # 悬浮文本

    right_click_menu=None,
    # 右击调出菜单

    visible=True,
    # 元素可见状态设定

    element_justification="left",
    # 框架内元素的对齐方式
    # 有效值 left,right,center

    vertical_alignment=None,
    # 垂直对齐方式
    # top bottom center
    # 支持首字母简写
       )
import  PySimpleGUI as sg

col_layout = [[sg.T("姓名",background_color="blue"),sg.In(size=(8,1))],
              [sg.T("性别",background_color="blue"),sg.In(size=(8,1))],
              [sg.T("国籍",background_color="blue"),sg.In(size=(8,1))],
              ]

layout = [[sg.Frame("基本信息",
                  col_layout,
                  background_color="blue",
                  key="-Col-",
                  title_location='s'
                  )],
          # [sg.LB(range(1,10),size=(100,100))]
          ]

event, value = sg.Window("pthon GUI", layout, keep_on_top=True, resizable=True).read(close=True)

update方法

Update(
        value=None, 
        # 框架元素的标题
        visible=None
        # 元素的可见状态
        )
import  PySimpleGUI as sg



frame1_layout = [
              [sg.T("姓名",size=(8,1)),sg.In()],
              [sg.T("性别",size=(8,1)),sg.In()],
              [sg.T("国籍",size=(8,1)),sg.In()]]
fram2_layout = [[sg.LB(list(range(15)),size=(20,6))]]
layout = [[sg.B("界面1"),sg.B("界面2")],
          [sg.Frame("基本信息", layout=frame1_layout, key='-fram1-'),
           sg.Frame("listbox",layout=fram2_layout,visible=False,key='-fram2-')],
          # [sg.LB(range(1,10),size=(100,100))]
          ]
window = sg.Window("pthon GUI", layout, keep_on_top=True, resizable=True)
result = True
while True:

    event, value = window.read()
    print(event)
    if event==None:
        break
    if event == "界面2":
        window['-fram2-'].update(visible=True)
        window['-fram1-'].update(visible=False)
    if event == "界面1":
        window['-fram1-'].update(visible=True)
        window['-fram2-'].update(visible=False)


window.close()
import  PySimpleGUI as sg



frame1_layout = [
              [sg.T("姓名",size=(8,1)),sg.In()],
              [sg.T("性别",size=(8,1)),sg.In()],
              [sg.T("国籍",size=(8,1)),sg.In()]]
fram2_layout = [[sg.LB(list(range(15)),size=(20,6))]]
layout = [[sg.B("界面切换")],
          [sg.Frame("基本信息", layout=frame1_layout, key='-fram1-'),
           sg.Frame("listbox",layout=fram2_layout,visible=False,key='-fram2-')],
          # [sg.LB(range(1,10),size=(100,100))]
          ]
window = sg.Window("pthon GUI", layout, keep_on_top=True, resizable=True)
result = True
while True:

    event, value = window.read()
    print(event)
    if event==None:
        break
    if event == "界面切换":
        window['-fram2-'].update(visible=result)
        window['-fram1-'].update(visible=not result)
        result = not result



window.close()

标签元素 Tab & TabGroup

sg.TabGroup(
           [[sg.Tab('Tab1',tab1_layout,),sg.Tab('Tab2',tab2_layout)]],
               )

注意事项:(1.TabGroup(layout), 2.Tab1和Tab2要写在一行里面。只能写Tab元素)

Tab(
    title,
    # str
    # 标签上显示的文本
    layout,
    # 标签内包含的布局

    background_color=None,
    # 标签的背景颜色设定

    font=None,
    # 字体名称,大小设定

    pad=None,
    # 周围元素的间隔设定

    disabled=False,
    # 禁用状态设定

    border_width=None,
    # 边界线宽度设定

    key=None,
    # 元素的唯一标识符

    tooltip=None,
    # 悬浮文本设定

    right_click_menu=None,
    # 右击调出菜单

    visible=True,
    # 元素可见状态设定

    element_justification="left",
    # 容器内元素的对齐方式
    # 有效值 left,right,center
       )
TabGroup(
    layout,
    # 标签组内的tab布局,注意sg.Tab()要写在第一行。

    tab_location=None,
    # 标签标题所处的位置
    # 有效值有12个,分别为left, right, top, bottom, lefttop, leftbottom, 
    # righttop, rightbottom, bottomleft, bottomright, topleft, topright

    title_color=None,
    # 标题文本颜色(未选中时)

    tab_background_color=None,
    # 所有标签背景颜色(未选中时)

    selected_title_color=None,
    # 选中时标题文本颜色

    selected_background_color=None,
    # 选中时标签背景颜色

    background_color=None,
    # 标签标题所在空白区域的背景颜色

    font=None,
    # 字体名称,大小设定

    enable_events=False,
    # 元素的事件属性
    # 切换标签元素就会发生事件。

    pad=None,
    # 和周围元素间隔的设定

    border_width=None,
    # 边界线宽度设定

    key=None,
    # 元素的唯一标识符

    tooltip=None,
    # 悬浮文本设定

    visible=True,
    # 元素可见状态设定

                 )
import  PySimpleGUI as sg


column_layout = [
              [sg.T("姓名"),sg.In(size=(3,1))],
              [sg.T("性别"),sg.In(size=(3,1))],
              [sg.T("国籍"),sg.In(size=(3,1))]]
table1_layout = [
              [sg.T("姓名",size=(8,1)),sg.In()],
              [sg.T("性别",size=(8,1)),sg.In()],
              [sg.T("国籍",size=(8,1)),sg.In()]]
table2_layout = [[sg.LB(list(range(15)),size=(20,6)),
                  sg.TabGroup([[sg.Tab("1",layout=[[sg.CB(i)] for i in range(2)]),
                  sg.Tab("2",layout=[[sg.Combo(list(range(15)),size=(10,3))]]),
                                ]]),
                  sg.Col(column_layout)],
                 [sg.Frame('slider',layout=[[sg.Slider(orientation='h',size=(50,10))]])]

                 ]


layout = [[sg.TabGroup([[sg.Tab("table1",table1_layout),sg.Tab("table2",table2_layout)]],
                       # tab_location="top",
                       title_color="grey",   # 未选中的tab的字体颜色
                       tab_background_color="black",  # 未选中的tab的标签背景样色
                       selected_title_color=None, # 选中时标题文本颜色
                       selected_background_color=None, # 选中时标签背景颜色
                       background_color="white",
                       border_width=0
                )] ]
window = sg.Window("pthon GUI", layout, keep_on_top=True, resizable=True)
result = True
while True:

    event, value = window.read()
    print(event)
    if event==None:
        break
    if event == "界面切换":
        window['-fram2-'].update(visible=result)
        window['-fram1-'].update(visible=not result)
        result = not result



window.close()

Update方法

Update(
    title=None, 
    # 标签标题文本的更新

    disabled=None,
    # 禁用状态更新

    visible=None
    # 可见状态更新
    )
import PySimpleGUI as sg


tab1_layout=[
        [sg.Text('姓名',size=(8,1)),sg.In()],
        [sg.Text('性别',size=(8,1)),sg.In()],
        [sg.Text('国籍',size=(8,1)),sg.In()]]

col_layout=[
        [sg.Text('姓名',size=(3,1)),sg.In(size=(3,1))],
        [sg.Text('性别',size=(3,1)),sg.In(size=(3,1))],
        [sg.Text('国籍',size=(3,1)),sg.In(size=(3,1))]]

tab2_layout=[[sg.LB(list(range(15)),size=(20,6)),
                sg.TabGroup([[sg.Tab('1',[[sg.CB(i)]for i in range(2)]),
                                sg.Tab('2',[[sg.Combo(list(range(15)),size=(10,3))]])]]),
            sg.Col(col_layout)],[
            sg.Frame('slider',[[sg.Slider(orientation='h',size=(50,10))]])]]

layout=[[sg.TabGroup([[ sg.Tab('tab1',tab1_layout,title_color='blue',),sg.Tab('tab2',tab2_layout) ]])]]

window=sg.Window('Python GUI',layout,keep_on_top=True)

result=True

while True:
    event,values=window.read()
    print(event)   
    if event==None:  
        break

window.close()

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值