sg.Window()里面的参数relative_location可以干什么
时间: 2024-04-10 14:30:04 浏览: 18
在CSDN开发中,`sg.Window()`是SimpleGUI库中创建窗口的函数,而`relative_location`是`sg.Window()`的一个参数,用于定义窗口的相对位置。它可以接受一个包含两个元素的元组或列表,表示窗口在屏幕上的相对位置。
具体来说,这个参数可以用来控制窗口的左上角相对于屏幕左上角的偏移量。例如,`relative_location=(100, 200)`表示窗口的左上角相对于屏幕左上角向右偏移100个像素,向下偏移200个像素。
通过调整`relative_location`参数,您可以控制窗口相对于屏幕的位置,使其出现在您希望的位置上。这对于创建多个窗口、窗口布局、或者根据用户偏好定位窗口等场景非常有用。
相关问题
sg.Window.set_background_color 帮我把这个弄成黑色
好的,你可以使用以下代码将窗口背景颜色设置为黑色:
```python
import PySimpleGUI as sg
sg.theme('DarkBlue')
layout = [[sg.Text('Hello, World!')]]
window = sg.Window('My Window', layout)
window.set_background_color('black')
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
window.close()
```
在上面的代码中,我们创建了一个名为window的窗口对象,并使用window.set_background_color('black')将窗口背景颜色设置为黑色。注意,这个设置必须在window.read()方法之前进行,否则不会生效。
另外,我们使用了sg.theme('DarkBlue')将应用程序的主题设置为深蓝色。这个设置可以让应用程序的其他组件(例如按钮、文本框等)与窗口背景颜色相协调,让应用程序看起来更加和谐。
报错修正: import PySimpleGUI as sg import pandas as pd # 定义窗口布局 layout = [ [sg.Text('选择表格文件')], [sg.Input(), sg.FileBrowse(key='file_1'), sg.Text('表格1')], [sg.Input(), sg.FileBrowse(key='file_2'), sg.Text('表格2')], [sg.Button('查找')], [sg.Text('查找结果:')], [sg.Multiline(size=(60, 10), key='result')], [sg.Button('保存结果')] ] # 创建窗口 window = sg.Window('查找表格数据', layout) # 读取数据并查找相同时间段的数据 def find_data(file_1, file_2): try: # 读取表格数据 df_1 = pd.read_csv(file_1) df_2 = pd.read_csv(file_2) # 找到相同时间段的数据 df = pd.merge(df_1, df_2, on='Time', how='inner') # 返回查找结果 return df.to_string(index=False) except Exception as e: return str(e) # 事件循环 while True: event, values = window.read() if event == sg.WINDOW_CLOSED: break elif event == '查找': # 获取用户选择的文件路径 file_1 = values['file_1'] file_2 = values['file_2'] # 查找数据 result = find_data(file_1, file_2) # 在窗口中显示查找结果 window['result'].update(result) elif event == '保存结果': # 获取当前显示的数据 table_values = window['result'].get() # 将数据保存到新的表格中 save_file = sg.popup_get_file('保存文件', save_as=True, default_extension='.csv') if save_file: save_df = pd.DataFrame(table_values[1:], columns=table_values[0]) save_df.to_excel(save_file, index=False) sg.popup('保存成功!')
这份代码有一个小问题,就是在保存结果的时候,需要将结果按照逗号分隔的形式传递给 DataFrame,而不是直接传递整个字符串。修改代码如下:
```python
import PySimpleGUI as sg
import pandas as pd
# 定义窗口布局
layout = [
[sg.Text('选择表格文件')],
[sg.Input(), sg.FileBrowse(key='file_1'), sg.Text('表格1')],
[sg.Input(), sg.FileBrowse(key='file_2'), sg.Text('表格2')],
[sg.Button('查找')],
[sg.Text('查找结果:')],
[sg.Multiline(size=(60, 10), key='result')],
[sg.Button('保存结果')]
]
# 创建窗口
window = sg.Window('查找表格数据', layout)
# 读取数据并查找相同时间段的数据
def find_data(file_1, file_2):
try:
# 读取表格数据
df_1 = pd.read_csv(file_1)
df_2 = pd.read_csv(file_2)
# 找到相同时间段的数据
df = pd.merge(df_1, df_2, on='Time', how='inner')
# 返回查找结果
return df
except Exception as e:
return str(e)
# 事件循环
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == '查找':
# 获取用户选择的文件路径
file_1 = values['file_1']
file_2 = values['file_2']
# 查找数据
result = find_data(file_1, file_2)
# 在窗口中显示查找结果
window['result'].update(result.to_string(index=False))
elif event == '保存结果':
# 获取当前显示的数据
table_values = window['result'].get().split('\n')
table_values = [row.split(',') for row in table_values]
# 将数据保存到新的表格中
save_file = sg.popup_get_file('保存文件', save_as=True, default_extension='.csv')
if save_file:
save_df = pd.DataFrame(table_values[1:], columns=table_values[0])
save_df.to_csv(save_file, index=False)
sg.popup('保存成功!')
```
这样就可以正确地保存结果了。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)