我如何在python中使用接口连接程序

时间:2017-12-06 06:23:31

标签: python tkinter

我想使用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)))

我想使用值(a)和值(list(zip(b,wordfreq)) 我怎么能给出Button值?

3 个答案:

答案 0 :(得分:0)

如果要进行彼此“交谈”的进程,可以使用Python标准库中的子进程模块。

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

将代码放入函数内的脚本中,并将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()

BTW:您可以使用参数

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

但您可以使用更多参数创建函数并执行

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

如果您需要此函数中的某些值,请使用函数

return value1, value2 

然后您可以将其用作

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