PySimpleGUI 怎么把打印出来,展示到UI上
时间: 2023-05-21 22:05:50 浏览: 77
您可以使用PySimpleGUI中的Output控件来实现将打印内容展示到UI上。以下是一些示例代码:
```
import PySimpleGUI as sg
# 创建布局,添加Output控件
layout = [[sg.Output(size=(60,20))],
[sg.Button('Print Something'), sg.Exit()]]
# 创建窗口
window = sg.Window('Demo', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Exit':
break
elif event == 'Print Something':
print('Hello, World!')
# 将打印结果输出到Output控件
window['Output'].print('Hello, World!')
window.close()
```
在上面的示例代码中,我们使用了Output控件来展示打印结果,当点击"Print Something"按钮时,除了将结果打印到控制台外,还将其输出到Output控件上。
相关问题
PySimpleGUI怎么将控制台输出打印到UI界面
你可以使用PySimpleGUI的`Print()`函数将控制台输出打印到UI界面上的文本框中。你需要先创建一个文本框,然后在需要输出的地方调用`Print()`函数,将输出内容作为参数传入,即可将内容输出到文本框中。
下面是一个简单的例子,演示如何将控制台输出打印到UI界面上的文本框中:
```python
import PySimpleGUI as sg
layout = [[sg.Text("这里是输出:")],
[sg.Output(size=(60, 10))]] # 创建一个文本框用于输出
window = sg.Window("输出到UI界面示例", layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
print("这是一条输出信息") # 将输出打印到文本框中
window.close()
```
运行代码后,你会看到一个UI界面,上面有一个文本框,控制台输出会被打印到文本框中。
pysimplegui怎么将控制台的输出到UI界面
可以使用PySimpleGUI的`Output`元素来实现将控制台的输出显示在UI界面上。以下是一个简单的示例代码:
```python
import PySimpleGUI as sg
# 创建UI界面布局
layout = [[sg.Multiline(size=(60, 10), key='-OUTPUT-')],
[sg.Button('Print Something')]]
# 创建窗口
window = sg.Window('Console Output to UI', layout)
# 事件循环
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == 'Print Something':
# 将控制台输出重定向到UI界面的Output元素
print('Hello, World!', file=values['-OUTPUT-'])
# 关闭窗口
window.close()
```
在这个示例中,我们创建了一个`Multiline`元素作为输出区域,并将其键值设置为`-OUTPUT-`。然后,在按钮被点击时,我们使用Python的`print()`函数将输出内容重定向到`-OUTPUT-`键对应的元素上。
请注意,我们在`print()`函数中使用了`file=values['-OUTPUT-']`参数,这是将输出重定向到UI界面的关键。在PySimpleGUI中,`Output`元素可以被视为一个文件对象,因此我们可以使用文件输出模式来将内容写入该元素上。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)