Tkinter错误:+:'instance'和'str'不支持的操作数类型

时间:2017-01-19 21:17:31

标签: python user-interface tkinter

我试图让用户浏览文件的目录位置以获取excel数据。 到目前为止,broswe功能工作并显示为标签,但尝试将标签返回到任何按钮选项(如texas())的功能不起作用。 我从shell获得的以下错误是:

  

“不支持的操作数类型+:'实例'和'str'”

那么如何将标签值转换为字符串并使其有效?

代码:

from Tkinter import *

import tkFileDialog

import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk

import os
import tkMessageBox
root = Tk.Tk()
root.title("Excel Map Conversion")
root.geometry("450x400")# width x height

def browse_gainInfo():
    currentDir = os.getcwd()
    fname = tkFileDialog.askdirectory(parent=root, initialdir=currentDir, title="Please Select the Folder for Map Data Input")
    pathlabel.config(text=fname)

filepath = StringVar()

def texas():
    os.startfile(browseLink + '/Texas/Texas_conversion.py')

def state():
    os.startfile(filepath.get() + '\State\StateConv.py')

def college():

    os.startfile(filepath.get() + '\College\CollegeConv.py')

def underGrad():
    os.startfile(filepath.get() + '\Undergraduate\undergrad_conversion.py')

def grad():
    os.startfile(filepath.get()+ '\Graduate\graduate_conversion.py')

browseLabel = Label(root, text="Browse for source of Map Data").pack()
browse1 = Tk.Button(root, text='Browse', width =6, command = browse_gainInfo)
browse1.pack()
pathlabel= Label(root)
pathlabel.pack()

browseLink = StringVar(pathlabel)

label = Label(root, text="\nEnter in the filepath for the map files: \n").pack()
link = Entry(root, textvariable = filepath).pack()


label2 = Label(root, text ="\n\tPlease select from the following options:\n").pack()

button1 = Button(root, text = "College", command = college).pack()

button2 = Button(root, text= "State", command = state).pack()

button3 = Button(root, text="Texas", command = texas).pack()

button4 = Button(root, text ="Undergraduate", command = underGrad).pack()

button5 = Button(root, text ="Graduate", command = grad).pack()



#kick off the event loop
root.mainloop()

1 个答案:

答案 0 :(得分:0)

来自tkinterbook:

  

请注意,StringVar()构造函数采用可选的小部件   论证,但没有价值论证;要设置该值,请调用该集   方法:

var = StringVar()
var.set("hello")
     

构造函数参数仅在运行Tkinter时才有意义   有多个Tk实例(你不应该这样做,除非你真的   知道你在做什么。

对于您正在做的事情,这可能是不必要的。我不是100%你正在尝试做什么......但似乎你正试图以迂回的方式创建textvariable