简体   繁体   English

tkinter 模块中的语法错误。 root = tk() TypeError: 'module' 对象不可调用

[英]Syntax error in tkinter module . root = tk() TypeError: 'module' object is not callable

I'm new to python but what I am trying to do is create a GUI which asks input from the user and when the user is finished, they select 'submit' which exports the data to an excel spreadsheet.我是 python 的新手,但我想做的是创建一个 GUI,它要求用户输入,当用户完成后,他们选择“提交”,将数据导出到 excel 电子表格。

The problem that keeps coming up is this error:不断出现的问题是这个错误:

  File "C:/Users/ptrovato/Desktop/UserForminPython_121019.py", line 144, in <module>
    root = tk()

TypeError: 'module' object is not callable

That is the error I am getting.那是我得到的错误。 Here is my entire code:这是我的整个代码:

import tkinter as tk 

from openpyxl import *


# globally declare wb and sheet variable 

# opening the eTypeError: 'module' object is not callablexisting excel file 
wb = load_workbook('C:\\Users\\ptrovato\\Desktop\\Python\\excel.xlsx') 

# create the sheet object 
sheet = wb.active 


def excel(): 

    # resize the width of columns in 
    # excel spreadsheet 
    sheet.column_dimensions['A'].width = 30 # Agency
    sheet.column_dimensions['B'].width = 10 # Department
    sheet.column_dimensions['C'].width = 10 # Project Title
    sheet.column_dimensions['D'].width = 20 # Description 
    sheet.column_dimensions['E'].width = 20 # Justification/Benefit
    sheet.column_dimensions['F'].width = 40 # Project Type
    sheet.column_dimensions['G'].width = 50 # New/Replacement
    sheet.column_dimensions['H'].width = 60 # Light/Heavy Duty

    # write given data to an excel spreadsheet 
    # at particular location 
    sheet.cell(row=1, column=1).value = "Agency"
    sheet.cell(row=1, column=2).value = "Department"
    sheet.cell(row=1, column=3).value = "Project_Title"
    sheet.cell(row=1, column=4).value = "Description"
    sheet.cell(row=1, column=5).value = "Justification_Benefits"
    sheet.cell(row=1, column=6).value = "Project_Type"
    sheet.cell(row=1, column=7).value = "New_or_Replacement"
    sheet.cell(row=1, column=8).value = "Light_or_Heavy_Duty_Equipment"

# Function to set focus (cursor) 
def focus1(event): 
    # set focus on the course_field box 
    Agency_field.focus_set() 

def focus2(event): 
    # set focus on the course_field box 
    Department_field.focus_set() 

# Function to set focus 
def focus3(event): 
    # set focus on the sem_field box 
    Project_Title.focus_set() 

# Function to set focus 
def focus4(event): 
    # set focus on the form_no_field box 
    Description_field.focus_set() 

# Function to set focus 
def focus5(event): 
    # set focus on the contact_no_field box 
    Justification_and_Benefits_field.focus_set() 

  # Function to set focus 
def focus6(event): 
    # set focus on the email_id_field box 
    Project_Type_field.focus_set() 

  # Function to set focus 
def focus7(event):
    # set focus on the address_field box 
    New_or_Replacement.focus_set() 

def focus8(event): 
    # set focus on the address_field box 
    Light_or_Heavy_Duty_Equipment.focus_set() 

# Function for clearing the 
# contents of text entry boxes 

def clear(): 

    # clear the content of text entry box 
    Agency_field.delete(0, END) 
    Department_field.delete(0, END) 
    Project_Title_field.delete(0, END) 
    Description_field.delete(0, END) 
    Justification_and_Benefits_field.delete(0, END) 
    Project_Type_field.delete(0, END) 
    New_or_Replacement_field.delete(0, END) 
    Light_or_Heavy_Duty_Equipment_field.delete(0, END) 

# Function to take data from GUI  
# window and write to an excel file 
def insert(): 

    # if user not fill any entry 
    # then print "empty input" 
    if (Agency_field.get() == "" and
        Department_field.get() == "" and
        Project_Title_field.get() == "" and
        Description_field.get() == "" and
        Justification_and_Benefits_field.get() == "" and
        Project_Type_field.get() == "" and
        New_or_Replacement_field.get() == "" and
        Light_or_Heavy_Duty_Equipment_field.get() == ""): 

        print("empty input") 

    else: 

        # assigning the max row and max column 
        # value upto which data is written 
        # in an excel sheet to the variable 
        current_row = sheet.max_row 
        current_column = sheet.max_column 

        # get method returns current text 
        # as string which we write into 
        # excel spreadsheet at particular location 
        sheet.cell(row=current_row + 1, column=1).value = Agency_field.get() 
        sheet.cell(row=current_row + 1, column=2).value = Department_field.get() 
        sheet.cell(row=current_row + 1, column=3).value = Project_Title_field.get() 
        sheet.cell(row=current_row + 1, column=4).value = Description_field.get() 
        sheet.cell(row=current_row + 1, column=5).value = Justification_and_Benefits_field.get() 
        sheet.cell(row=current_row + 1, column=6).value = Project_Type_field.get() 
        sheet.cell(row=current_row + 1, column=7).value = New_or_Replacement_field.get() 
        sheet.cell(row=current_row + 1, column=8).value = Light_or_Heavy_Duty_Equipment_field.get() 
        # save the file 
        wb.save('C:\\Users\\ptrovato\\Desktop\\Python\\excel.xlsx') 

        # set focus on the name_field box 
        Agency_field.focus_set() 

        # call the clear() function 
        clear() 


# Driver code 
if __name__ == "__main__":

    # create a GUI window 
    root = tk()

    # set the background colour of GUI window 
    root.configure(bg='light green') 

    # set the title of GUI window 
    root.title("registration form")
    # set the configuration of GUI window 
    root.geometry("500x300")

    excel() 

    # create a Form label 
    heading = Label(root, text="Form", bg="Light green") 

    # create an Agency label 
    Agency = Label(root, text="Agency", bg="light green") 

    # create a Department label 
    Department = Label(root, text="Department", bg="light green") 

    # create a Project Title label 
    Project_Title = Label(root, text="Project Title", bg="light green") 

    # create a Description lable 
    Description = Label(root, text="Description", bg="light green") 

    # create a Justificiation label 
    Justification_and_Benefits = Label(root, text="Justification/Benefits", bg="light green")

    # create a Project Type: Capital, Vehicle, or Equipment label 
    Project_Type = Label(root, text="Project Type: Capital, Vehicle, or Equipment", bg="light green")

    # create a New/Replacement label 
    New_or_Replacement = Label(root, text="New/Replacement", bg="light green") 

    # create a Light/Heavy Dutys label 
    Light_or_Heavy_Duty_Equipment = Label(root, text="Light/Heavy Duty", bg="light green") 

    # grid method is used for placing 
    # the widgets at respective positions 
    # in table like structure . 
    heading.grid(row=0, column=1) 
    Agency.grid(row=1, column=0) 
    Department.grid(row=2, column=0) 
    Project_Title.grid(row=3, column=0) 
    Justification_and_Benefits.grid(row=4, column=0) 
    Project_Type.grid(row=5, column=0) 
    New_or_Replacement.grid(row=6, column=0) 
    Light_or_Heavy_Duty_Equipment.grid(row=7, column=0) 

    # create a text entry box 
    # for typing the information 
    Agency_field = Entry(root) 
    Department_field = Entry(root) 
    Project_Title_field = Entry(root) 
    Description_field = Entry(root) 
    Justification_and_Benefits_field = Entry(root) 
    Project_Type_field = Entry(root) 
    New_or_Replacement_field = Entry(root) 
    Light_or_Heavy_Duty_Equipment_field = Entry(root)

    # bind method of widget is used for 
    # the binding the function with the events 

    # whenever the enter key is pressed 
    # then call the focus1 function 
    Agency_field.bind("<Return>", focus1) 

    # whenever the enter key is pressed 
    # then call the focus2 function 
    Department_field.bind("<Return>", focus2) 

    # whenever the enter key is pressed 
    # then call the focus3 function 
    Project_Title_field.bind("<Return>", focus3) 

    # whenever the enter key is pressed 
    # then call the focus4 function 
    Description_field.bind("<Return>", focus4) 

    # whenever the enter key is pressed 
    # then call the focus5 function 
    Justification_and_Benefits_field.bind("<Return>", focus5) 

    # whenever the enter key is pressed 
    # then call the focus6 function 
    Project_Type_field.bind("<Return>", focus6) 

    # whenever the enter key is pressed 
    # then call the focus6 function 
    New_or_Replacement_field.bind("<Return>", focus7)

    # whenever the enter key is pressed 
    # then call the focus6 function 
    Light_or_Heavy_Duty_Equipment_field.bind("<Return>", focus8)

    # grid method is used for placing 
    # the widgets at respective positions 
    # in table like structure . 
    Agency_field.grid(row=1, column=1, ipadx="100") 
    Department_field.grid(row=2, column=1, ipadx="100") 
    Project_Title_field.grid(row=3, column=1, ipadx="100") 
    Description_field.grid(row=4, column=1, ipadx="100") 
    Justification_and_Benefits_field.grid(row=5, column=1, ipadx="100") 
    Project_Type_field.grid(row=6, column=1, ipadx="100") 
    New_or_Replacement_field.grid(row=7, column=1, ipadx="100")
    Light_or_Heavy_Duty_Equipment_field.grid(row=8, column=1, ipadx="100") 

    # call excel function 
    excel() 

    # create a Submit Button and place into the root window 
    submit = Button(root, text="Submit", fg="Black", 
                            bg="Yellow", command=insert) 
    submit.grid(row=8, column=1)


    # start the GUI 
    root.mainloop() 

If anyone knows the fix, I would really appreciate it!如果有人知道修复方法,我将不胜感激!

Can anyone help me?谁能帮我? Thanks!谢谢!

the tkinter module defines a class named Tk. tkinter 模块定义了一个名为 Tk 的类。 You have to correct it by:您必须通过以下方式更正它:

import tkinter as tk
root = tk.Tk()

or:要么:

from tkinter import Tk  
root = Tk()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM