-2

I want to use tkinter to set a interface for my another program, but I do not know what is my next step.

I do not know how to connect each other.

I do not have that concept.

This is the code of trying to be a interface:

#!/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()

This is the code of my python script:

#!/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)))

I want to use the value(a) and value(list(zip(b,wordfreq)) How could I give the Button value?

Steve
  • 73
  • 7
  • What exactly is it you want to do? – Nae Dec 06 '17 at 07:03
  • I need a interface that I can input the keyword, then it will show me which file has that word. – Steve Dec 06 '17 at 09:08
  • What is the difficulty you're having about that there? What is the variable name for keyword? – Nae Dec 06 '17 at 09:16
  • [This](https://stackoverflow.com/questions/30076185/call-python-script-with-input-with-in-a-python-script-using-subprocess) might help. – Nae Dec 06 '17 at 10:02
  • put code in secon file in function and `import` it To file with gui – furas Dec 06 '17 at 11:11

3 Answers3

0

If you want to processes to 'talk' to each other, you can use the subprocesses module in the Python Standard Library.

Jay Speidell
  • 170
  • 7
0

You have some files.py that have functions and classes.

Then, you have an ifmain in each files to test librairies you built. Because this is what you're doing at your own level.

You use, then, a main file to link all of this together. And in your main file you can add an optional argument that takes your program output, and... well. I guess you know what to do next. :)

IMCoins
  • 3,149
  • 1
  • 10
  • 25
0

Put code in script inside function and add if __name__ == "__main__" to the end

#!/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()

So it will still work as standalone script but now you can import it in second file and execute. I execut it in 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: you can execute my_function with parameter

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

but you can create function with more arguments and execute

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

If you need some values from this function then use in function

return value1, value2 

and then you can use it as

val1, val2 = my_function("/path/", a, b)
furas
  • 134,197
  • 12
  • 106
  • 148