TypeError:“ StringVar”类型的参数不可迭代

时间:2018-11-23 15:44:41

标签: python-3.x tkinter

我有一个Button浏览一个文本文件,另一个Button浏览一个exe文件。我的文本文件有一个IP列表,我要为其运行该数量的exe文件。当我有一个简单的控制台应用程序时,我的程序运行正常。现在,我试图通过使用Tkinter引入GUI来增强它,但是我遇到了问题。我使用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 :(得分:0)

运行此重构功能,可以正常运行。您忘记在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()

完整代码

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()