添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

如何在PySimpleGUI窗口中打印for i in range循环?

0 人关注

我想做一个程序,告诉你一个与你选择的单词押韵的输入量。我是在PySimpleGUI中做的,我不知道如何将循环打印到PySimpleGUI窗口中。谁能帮帮我。预先感谢 -Josh

import PySimpleGUI as sg
import requests
layout = [[sg.Text('What would you like to hear rhymes about: '), sg.InputText()],
      [sg.Submit(), sg.Cancel()]]
window = sg.Window('Rhyme.', layout)
event, values = window.read()
rhyme = values[0]
words = requests.get('https://api.datamuse.com/words?rel_rhy=' + rhyme)
words_json = words.json()
window.close()
layout = [[sg.Text(f'How many rhymes about {rhyme} would you like to hear: '), sg.InputText()],
          [sg.Submit(), sg.Cancel()]]
window = sg.Window('Rhyme.', layout)
event, values = window.read()
num = values[0] 
window.close()
layout = [[sg.Text(#I dont Know how to print the list here) 
    
1 个评论
你想打印 words_json 的内容?你没有得到押韵的词
python
pysimplegui
JoshMann15
JoshMann15
发布于 2020-06-01
3 个回答
Zain Arshad
Zain Arshad
发布于 2020-06-01
0 人赞同

如果你想在 Text() 中显示一个列表,你可以简单地使用 join() 。我认为你想显示的列表是 words_json ,但是,当我运行你的代码时,这个列表是空的。

然而,使用这个代码来给一个列表穿鞋。

layout = [[sg.Text(','.join(words_json))]]
    
Bhargav Desai
Bhargav Desai
发布于 2020-06-01
0 人赞同

this code will work for you.

import PySimpleGUI as sg
import requests
layout = [[sg.Text('What would you like to hear rhymes about: '), sg.InputText()],
      [sg.Submit(), sg.Cancel()]]
window = sg.Window('Rhyme.', layout)
event, values = window.read()
rhyme = values[0]
words = requests.get('https://api.datamuse.com/words?rel_rhy=' + rhyme)
words_json = words.json()
window.close()
layout = [[sg.Text(f'How many rhymes about {rhyme} would you like to hear: '), sg.InputText()],
          [sg.Submit(), sg.Cancel()]]
window = sg.Window('Rhyme.', layout)
event, values = window.read()
num = values[0] 
window.close()
layout = []
for x in words_json:
    layout = layout + [sg.Text(x['word'])]
layout = [layout]
window = sg.Window('word',layout)
event, values = window.read()
window.close()
    
r-beginners
r-beginners
发布于 2020-06-01
0 人赞同

这对我来说是一个很好的锻炼。 Github 我定制了这个作为参考。

import PySimpleGUI as sg
import requests
from bs4 import BeautifulSoup
import json
def get_rhyme(rhyme):
    url = ('https://api.datamuse.com/words?rel_rhy={}').format(rhyme)
    words = requests.get(url)
    soup = BeautifulSoup(words.text, 'lxml')
    words_json = []
    words_json = words.json()
    return words_json
def gui():
    layout = [[sg.Text('What would you like to hear rhymes about: ', size=(20,1))],
              [sg.Input('', key='-INPUT-'), sg.Submit(), sg.Cancel()],
              [sg.Output(size=(50,50), key='-OUTPUT-')],
              [sg.Button('Clear'), sg.Button('Exit')]]
    window = sg.Window('Rhyme Search beta', keep_on_top=True, layout=layout)
    output_elem = window.FindElement('-OUTPUT-')
    while True:
        event, values = window.Read()    # read with a 5 second timeout
        print(event, values)
        if event in ('Exit', None):
            break
        elif event == 'Submit':
            out_json = get_rhyme(values['-INPUT-'])
            output_elem.update(out_json)
        elif event == 'Clear':
            window['-OUTPUT-'].update('')