【问题标题】:How could I connect a program with interface in python如何在 python 中将程序与接口连接起来
【发布时间】:2017-12-06 06:23:30
【问题描述】:

我想使用 tkinter 为我的另一个程序设置接口,但我不知道下一步是什么。

我不知道如何相互连接。

我没有这个概念。

这是尝试做接口的代码:

#!/usr/bin/env python -O
import subprocess
from tkinter import *
subprocess.call('/Users/Tsu-
AngChou/MasterProject/Practice/try_test/TEST5.py')

root = Tk(className ="Documents retriever")
svalue = StringVar() # defines the widget state as string
w = Entry(root,textvariable=svalue) # adds a textarea widget
w.pack()
def act():
    print ("you entered")
    print ('%s' % svalue.get())
foo = Button(root,text="Retrieve", command=act)
foo.pack()
root.mainloop()

这是我的python脚本的代码:

#!/usr/bin/env python
import string
import os
from bs4 import BeautifulSoup as bs
from os import listdir
from os.path import isfile, join


mypath = "/Users/Tsu-AngChou/MasterProject/Practice/try_test/"
files = listdir(mypath)
storage = {}
translator = str.maketrans("","",string.punctuation)
for f in files:
  fullpath = join(mypath, f)
  if f == '.DS_Store':
            os.remove(f)
  elif isfile(fullpath):

    print(f)
    for html_cont in range(1):
        response = open(f,'r',encoding='utf-8')
        html_cont = response.read()
        soup = bs(html_cont, 'html.parser')
        regular_string = soup.get_text()
        new_string = regular_string.translate(translator).split()
        new_list = [item[:14] for item in new_string]
        a = dict.fromkeys(new_list, f)
        b = a
        storage.update(a)
        print(a)
        storage.append(new_list)

        wordfreq = []
        for w in new_list:
            wordfreq.append(new_list.count(w))
        print("Frequency:\n" ,list(zip(b,wordfreq)))

我想使用 value(a) 和 value(list(zip(b,wordfreq)) 我怎样才能给 Button 值?

【问题讨论】:

  • 你到底想做什么?
  • 我需要一个可以输入关键字的界面,然后它会告诉我哪个文件有这个词。
  • 您在这方面遇到了什么困难?关键字的变量名是什么?
  • This 可能会有所帮助。
  • 把代码放到函数中的secon文件中,import它用gui归档

标签: python tkinter


【解决方案1】:

如果您希望进程相互“对话”,您可以使用 Python 标准库中的 subprocesses 模块。

【讨论】:

    【解决方案2】:

    您有一些具有函数和类的 files.py。

    然后,您在每个文件中都有一个 ifmain 来测试您构建的库。因为这是你在自己的水平上所做的事情。

    然后,您使用一个主文件将所有这些链接在一起。在您的主文件中,您可以添加一个可选参数来获取您的程序输出,并且......好吧。我想你知道下一步该做什么了。 :)

    【讨论】:

      【解决方案3】:

      将代码放入函数内部的脚本中,并在末尾添加if __name__ == "__main__"

      #!/usr/bin/env python
      import string
      import os
      from bs4 import BeautifulSoup as bs
      from os import listdir
      from os.path import isfile, join
      
      def my_function(mypath = "/Users/Tsu-AngChou/MasterProject/Practice/try_test/"):
          files = listdir(mypath)
          storage = {}
          translator = str.maketrans("","",string.punctuation)
          for f in files:
            fullpath = join(mypath, f)
            if f == '.DS_Store':
                      os.remove(f)
            elif isfile(fullpath):
      
              print(f)
              for html_cont in range(1):
                  response = open(f,'r',encoding='utf-8')
                  html_cont = response.read()
                  soup = bs(html_cont, 'html.parser')
                  regular_string = soup.get_text()
                  new_string = regular_string.translate(translator).split()
                  new_list = [item[:14] for item in new_string]
                  a = dict.fromkeys(new_list, f)
                  b = a
                  storage.update(a)
                  print(a)
                  storage.append(new_list)
      
                  wordfreq = []
                  for w in new_list:
                      wordfreq.append(new_list.count(w))
                  print("Frequency:\n" ,list(zip(b,wordfreq)))
      
      if __name__ == '__main__':
         my_function()
      

      所以它仍然可以作为独立脚本工作,但现在您可以在第二个文件中 import 它并执行。我在act()执行它

      #!/usr/bin/env python -O
      
      from tkinter import *
      from my_scipt import my_function
      
      def act():
          print("you entered")
          print(svalue.get())
          my_function()
      
      root = Tk()
      svalue = StringVar()
      w = Entry(root, textvariable=svalue)
      w.pack()
      foo = Button(root, text="Retrieve", command=act)
      foo.pack()
      root.mainloop()
      

      顺便说一句:你可以用参数执行my_function

      my_function("/path/to/different/folder")
      

      但您可以创建具有更多参数的函数并执行

      my_function("/path/", a, b)
      

      如果你需要这个函数的一些值,那么在函数中使用

      return value1, value2 
      

      然后你就可以把它当作

      val1, val2 = my_function("/path/", a, b)
      

      【讨论】:

        猜你喜欢
        • 2020-11-15
        • 2014-08-08
        • 2016-09-08
        • 2017-06-03
        • 2017-06-08
        • 2020-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多