TypeError:'Tk'对象不可调用

时间:2015-07-02 08:38:18

标签: python tkinter

我正在编写一个程序,该程序应该连接到服务器,检查DB并查找特定的表(以及这些表中有多少项),并且我正在使用Tkinter进行GUI。 问题是每当我尝试使用它时,我都会出现错误“TypeError:'Tk'Object is not callable”。 该程序曾经工作过,但现在已不复存在了,您对此有何了解? (不要担心这些奇怪的名字,我做了这个,所以我确定没有问题,比如一个与函数同名的变量)

import pyodbc
import sys
from tkinter import *

#wrote down variables here so I remembered them
username = ''
password = ''
server_name = ''
database = ''
table = 'véhicules'
nom_instance = ''
total = ''
nb_vehic = ''
master = Tk()

Label(master,text="What is the username ?").pack()
yzx = Entry(master)
yzx.pack()
username = yzx.get()
yzx.delete(0, END)

Label(master,text="What is this user's password?").pack()
yxz = Entry(master)
yxz.pack()
password = yxz.get()
yxz.delete(0, END)

Label(master, text="What is this server's IP adress ?").pack()
zyx = Entry(master)
zyx.pack()
server_name = zyx.get()
zyx.delete(0, END)

Label(master, text="which authority do you want to connect to ?").pack() #not sure about the traduction of this word, original is instance in french
xyz = Entry(master)
xyz.pack()
nom_instance=xyz.get()
xyz.delete(0, END)


def surelydoesntexist():
    connexion = "DRIVER={SQL Server};SERVER="+server_name+"\\"+nom_instance+";Database=master;User Id="+username+";Password="+password #peut être ajouter Trusted_Connection=yes
#at this point, connect to the server and looks for the different DBs
    connect = pyodbc.connect(connexion)
    cursor = connect.cursor()

    for db in cursor.execute("select * from sys.databases"):
        try:
            connexion = ("DRIVER={SQL Server};SERVER="+server_name+"\\"+nom_instance+";DATABASE="+db.name+";User Id="+username+";Password="+password)
            print ('%s ' % connexion)
            connect = pyodbc.connect(connexion)
            cursor = connect.cursor()
            cursor.execute("select count(*) from véhicules")
            row = cursor.fetchone()
            nb_vehic = row.user_count
            print ('%d items in this db' % row.user_count)
            total = total + int(nb_vehic)
        except: # catch *all* exceptions
            e = sys.exc_info()[0]
            print( "Error: %s" % e )

but = Button(master, text="Connect !", command=surelydoesntexist).pack()

master()

1 个答案:

答案 0 :(得分:2)

您的代码完全正常,但最后一行除外:

而不是master()你需要写master.mainloop()而一切都应该没问题。

备注:我对此进行了测试,但我已取消注释import pyodbc,功能surelydoesntexist和按钮but,因为我既没有所需的包也没有连接到服务器。< / p>