Ошибка типа: аргумент типа 'StringVar' не повторяется - PullRequest
0 голосов
/ 23 ноября 2018

У меня есть Button, который просматривает текстовый файл, и другой Button, который просматривает исполняемый файл.В моем текстовом файле есть список IP-адресов, для которых я хочу запустить такое количество исполняемых файлов.Когда у меня было простое консольное приложение, моя программа работала нормально.Теперь, когда я попытался улучшить его, введя графический интерфейс с использованием Tkinter, у меня возникли проблемы.Я использую StringVar и задаю для него строку, возвращаемую из filedialog.askopenfilename, но когда я пытаюсь перебрать ipFilePath.get, который является моим StringVar, он говорит TypeError: аргумент типа 'StringVar' не повторяется

Вот мой код:

import os
import subprocess
from tkinter import *
from tkinter import filedialog


#FUNCTIONS
def browsefunc():
ipFilePath.set(filedialog.askopenfilename(filetypes=[("IP file","*.txt")]))

def browsefunc2():
exeFilePath.set(filedialog.askopenfilename(filetypes=[("Program file", 
"*.exe")]))

def run():
with open(ipFilePath.get()) as f:
    for each_ip in f.readlines():
       p = subprocess.Popen([exeFilePath, each_ip.rstrip()], 
       stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
       p.communicate()
#GUI

root = Tk()

root.title('Map_Launcher')
root.geometry("698x150")

mf = Frame(root)
mf.pack()

f1 = Frame(mf, width=600, height=250) #file1
f1.pack(fill=X)
f2 = Frame(mf, width=600, height=250) #file2
f2.pack(fill=X)
f4 = Frame(mf, width=600, height=250) #run button
f4.pack()

ipFilePath = StringVar()
exeFilePath = StringVar()

Label(f1,text="Select file 1 (Only txt files)").grid(row=0, column=0, 
sticky='e') #file1 button
entry1 = Entry(f1, width=50, textvariable=ipFilePath)
entry1.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)

Label(f2,text="Select file 2 (Only exe files)").grid(row=0, column=0, 
sticky='e') #file2 button
entry2 = Entry(f2, width=50, textvariable=exeFilePath)
entry2.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)


Button(f1, text="Browse", command=browsefunc).grid(row=0, column=27, 
sticky='ew', padx=8, pady=4)#file1 button
Button(f2, text="Browse", command=browsefunc2).grid(row=0, column=27, 
sticky='ew', padx=8, pady=4)#file2 button
Button(f4, text="Run", width=32, command=run).grid(sticky='ew', 
padx=10, pady=10)#run button


root.mainloop()

1 Ответ

0 голосов
/ 23 ноября 2018

Запустите эту функцию рефакторинга, она работает нормально.Вы забыли принести get() в конце exeFilePath. Потому что вы не получили содержимое в записи, которое оно видит пустым.

def run():
    with open(ipFilePath.get()) as f:
        for each_ip in f.readlines():
            p = subprocess.Popen([exeFilePath.get(), each_ip.rstrip()],
                                 stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            p.communicate()

FULL CODE

import os
import subprocess
from tkinter import *
from tkinter import filedialog


#FUNCTIONS
def browsefunc():
    ipFilePath.set(filedialog.askopenfilename(filetypes=[("IP file", "*.txt")]))


def browsefunc2():
    exeFilePath.set(filedialog.askopenfilename(filetypes=[("Program file",
                                                           "*.exe")]))


def run():
    with open(ipFilePath.get()) as f:
        for each_ip in f.readlines():
            p = subprocess.Popen([exeFilePath.get(), each_ip.rstrip()],
                                 stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            p.communicate()


#GUI

root = Tk()

root.title('Map_Launcher')
root.geometry("698x150")

mf = Frame(root)
mf.pack()

f1 = Frame(mf, width=600, height=250) #file1
f1.pack(fill=X)
f2 = Frame(mf, width=600, height=250) #file2
f2.pack(fill=X)
f4 = Frame(mf, width=600, height=250) #run button
f4.pack()

ipFilePath = StringVar()
exeFilePath = StringVar()

Label(f1,text="Select file 1 (Only txt files)").grid(row=0, column=0,
sticky='e') #file1 button
entry1 = Entry(f1, width=50, textvariable=ipFilePath)
entry1.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)

Label(f2,text="Select file 2 (Only exe files)").grid(row=0, column=0,
sticky='e') #file2 button
entry2 = Entry(f2, width=50, textvariable=exeFilePath)
entry2.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)


Button(f1, text="Browse", command=browsefunc).grid(row=0, column=27,
sticky='ew', padx=8, pady=4)#file1 button
Button(f2, text="Browse", command=browsefunc2).grid(row=0, column=27,
sticky='ew', padx=8, pady=4)#file2 button
Button(f4, text="Run", width=32, command=run).grid(sticky='ew',
padx=10, pady=10)#run button


root.mainloop()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...