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

2024-04-19 09:40:44 发布

您现在位置:Python中文网/ 问答频道 /正文

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

"unsupported operand type(s) for +: 'instance' and '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()

Tags: thetextpyimportforosdefbutton
1条回答
网友
1楼 · 发布于 2024-04-19 09:40:44

来自tkinterbook:

Note that the StringVar() constructor takes an optional widget argument, but no value argument; to set the value, call the set method:

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

The constructor argument is only relevant if you’re running Tkinter with multiple Tk instances (which you shouldn’t do, unless you really know what you’re doing).

这可能对你所做的没有必要。我也不是你想做的那样。。。但似乎您试图以迂回的方式创建textvariable。在

相关问题 更多 >