<legend id='UyeQU'><style id='UyeQU'><dir id='UyeQU'><q id='UyeQU'></q></dir></style></legend>

<i id='UyeQU'><tr id='UyeQU'><dt id='UyeQU'><q id='UyeQU'><span id='UyeQU'><b id='UyeQU'><form id='UyeQU'><ins id='UyeQU'></ins><ul id='UyeQU'></ul><sub id='UyeQU'></sub></form><legend id='UyeQU'></legend><bdo id='UyeQU'><pre id='UyeQU'><center id='UyeQU'></center></pre></bdo></b><th id='UyeQU'></th></span></q></dt></tr></i><div id='UyeQU'><tfoot id='UyeQU'></tfoot><dl id='UyeQU'><fieldset id='UyeQU'></fieldset></dl></div>
  • <tfoot id='UyeQU'></tfoot>

    • <bdo id='UyeQU'></bdo><ul id='UyeQU'></ul>
    1. <small id='UyeQU'></small><noframes id='UyeQU'>

        将脚本连接/处理到 PySimpleGUI 按钮

        时间:2023-09-03
        <tfoot id='dT0SB'></tfoot>
          <bdo id='dT0SB'></bdo><ul id='dT0SB'></ul>
          1. <small id='dT0SB'></small><noframes id='dT0SB'>

                <i id='dT0SB'><tr id='dT0SB'><dt id='dT0SB'><q id='dT0SB'><span id='dT0SB'><b id='dT0SB'><form id='dT0SB'><ins id='dT0SB'></ins><ul id='dT0SB'></ul><sub id='dT0SB'></sub></form><legend id='dT0SB'></legend><bdo id='dT0SB'><pre id='dT0SB'><center id='dT0SB'></center></pre></bdo></b><th id='dT0SB'></th></span></q></dt></tr></i><div id='dT0SB'><tfoot id='dT0SB'></tfoot><dl id='dT0SB'><fieldset id='dT0SB'></fieldset></dl></div>

                  <tbody id='dT0SB'></tbody>

                1. <legend id='dT0SB'><style id='dT0SB'><dir id='dT0SB'><q id='dT0SB'></q></dir></style></legend>
                  本文介绍了将脚本连接/处理到 PySimpleGUI 按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  你们能帮我知道如何在我的 PySimpleGui 脚本中连接一个按钮,当按下/单击运行按钮时,它将执行另一个 python 脚本.

                  can you guys help me to know how to connect a button in my PySimpleGui script which will execute another python script when the run button is pressed/clicked.

                  目前,我一直在阅读有关 Subprocess 和 command = os.popen 的 GUI 脚本.

                  For now, i've been reading about Subprocess and command = os.popen in a GUI script.

                  layout = [[ sg.Text('Click the button to launch Program')],
                             [sg.Button('Launch')]]
                  
                  win1 = sg.Window('My new window').Layout(layout)
                  
                  
                  win2_activate = False
                  
                  while True:
                      ev1, vals1 = win1.Read()
                      if ev1 is None or ev1 == 'Cancel':
                          break
                  
                      if not win2_activate and ev1 == 'Launch':
                          win1.Hide()
                          win2_activate = True
                          layout2 = [[sg.Text('Report Auto')],
                                      [sg.Input(do_not_clear=True)],
                                      [sg.Text('', key='_OUTPUT_')],
                                      [sg.Button('Run'), sg.Button('Cancel')]]
                  
                          win2 = sg.Window('Window2').Layout(layout2)
                          while True:        
                              ev2, vals2 = win2.Read()
                              if ev2 is None or ev2 =='Cancel':
                                  win2_activate = False
                                  win2.Close()
                                  win1.UnHide()
                                  break
                  

                  在我的 pysimplegui 脚本中,我还没有包含子进程或任何库,因为我只是不知道在哪里做.欢迎任何帮助!

                  In my pysimplegui script, i have not yet included the subprocess or any library because i just don't know where to do it. Any help will is most welcome!

                  推荐答案

                  这里是您问题的完整答案,一个 PySimpleGUI 程序.该程序允许您输入命令.然后按一个按钮.按下按钮时,命令为运行",输出显示在窗口中.

                  Here's a full answer to your question, a PySimpleGUI program. This program allows you to type in a command. Then press a button. When button pressed the command is "run" and the output is shown in the window.

                  import subprocess
                  import sys
                  import PySimpleGUI as sg
                  
                  def main():
                      layout = [  [sg.Text('Enter a command to execute (e.g. dir or ls)')],
                                  [sg.Input(key='_IN_')],             # input field where you'll type command
                                  [sg.Output(size=(60,15))],          # an output area where all print output will go
                                  [sg.Button('Run'), sg.Button('Exit')] ]     # a couple of buttons
                  
                      window = sg.Window('Realtime Shell Command Output', layout)
                  
                      while True:             # Event Loop
                          event, values = window.Read()
                          if event in (None, 'Exit'):         # checks if user wants to exit
                              break
                  
                          if event == 'Run':                  # the two lines of code needed to get button and run command
                              runCommand(cmd=values['_IN_'], window=window)
                  
                      window.Close()
                  
                  # This function does the actual "running" of the command.  Also watches for any output. If found output is printed
                  def runCommand(cmd, timeout=None, window=None):
                      p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                      output = ''
                      for line in p.stdout:
                          line = line.decode(errors='replace' if (sys.version_info) < (3, 5) else 'backslashreplace').rstrip()
                          output += line
                          print(line)
                          window.Refresh() if window else None        # yes, a 1-line if, so shoot me
                      retval = p.wait(timeout)
                      return (retval, output)                         # also return the output just for fun
                  
                  if __name__ == '__main__':
                      main()
                  
                  

                  这篇关于将脚本连接/处理到 PySimpleGUI 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:当子进程不退出时,Python 的 subprocess.Popen 对象挂起收集子输出 下一篇:Python 3 子进程模块在运行“dir"时抛出错误.在 Windows 上

                  相关文章

                    <bdo id='ln5xc'></bdo><ul id='ln5xc'></ul>

                  <tfoot id='ln5xc'></tfoot>
                    <i id='ln5xc'><tr id='ln5xc'><dt id='ln5xc'><q id='ln5xc'><span id='ln5xc'><b id='ln5xc'><form id='ln5xc'><ins id='ln5xc'></ins><ul id='ln5xc'></ul><sub id='ln5xc'></sub></form><legend id='ln5xc'></legend><bdo id='ln5xc'><pre id='ln5xc'><center id='ln5xc'></center></pre></bdo></b><th id='ln5xc'></th></span></q></dt></tr></i><div id='ln5xc'><tfoot id='ln5xc'></tfoot><dl id='ln5xc'><fieldset id='ln5xc'></fieldset></dl></div>

                      <small id='ln5xc'></small><noframes id='ln5xc'>

                      <legend id='ln5xc'><style id='ln5xc'><dir id='ln5xc'><q id='ln5xc'></q></dir></style></legend>