我一直收到一个错误,说当我按下按钮时,按钮无法调用函数。这里是它无法运行的函数
import re
from tkinter import *
from tkinter import ttk
import time
from turtle import bgcolor
import webbrowser
from tkinter import messagebox
from tkinter import filedialog
root = Tk()
root.title('Notepad')
root.iconbitmap('C:/Users/Hero/Documents/Visual Studio code/My project/notes.ico')
root.geometry("640x500")
root.tk.call("source", "C:/Users/Hero/Documents/Visual Studio code/My project/azure.tcl")
root.tk.call("set_theme", "dark")
global x
x = 20
def change_theme():
if root.tk.call("ttk::style", "theme", "use") == "azure-dark":
root.tk.call("set_theme", "light")
else:
root.tk.call("set_theme", "dark")
def increase_font_size():
global x
global f
x = x + 1
print(x)
text.config(font=(f, x))
def decrease_font_size():
global x
global f
x = x - 1
print(x)
text.config(font=(f, x))
def pfonts():
font_page = Tk()
font_page.title('Fonts')
font_page.geometry('295x200')
font_page.configure(bg='#292929')
font_page.tk.call("source", "C:/Users/Hero/Documents/Visual Studio code/My project/azure.tcl")
font_page.tk.call("set_theme", "dark")
global f
fentry1 = ttk.Entry(font_page, width=80)
fentry1.pack()
label1 = Label(font_page, text='Enter the font name you want', font=("Tekton Pro", 17), bg='#292929')
label2 = Label(font_page, text='warning: caps lock sensitive', font=("Tekton Pro", 8), bg='#292929')
label1.pack()
label2.pack(side=BOTTOM)
def change_font():
global f
f = fentry1.get()
text.config(font=(f, x))
print(f)
bbfont = ttk.Button(font_page, text='Confirm font', command=change_font)
bbfont.pack(side=BOTTOM)
def killer():
print("killing the instence")
root.destroy()
def savefile():
global filename
filename = filedialog.asksaveasfilename(initialdir='/', title='Save File', filetypes=(('Text Files', '*.txt'), ('All Files', '*.*')))
textContent = text.get("1.0",END)
textContent = str(textContent)
myfile = open(filename, "w+")
myfile.write(textContent)
style=ttk.Style()
style.theme_use('azure-dark')
style.configure("Vertical.TScrollbar", background="grey", bordercolor="black", arrowcolor="white")
barframe = Frame()
barframe.pack(side=BOTTOM)
bfonts = ttk.Button(barframe, text='Fonts', style='Accent.TButton', command=pfonts)
open = ttk.Button(barframe, text='Open', style='Accent.TButton')
save = ttk.Button(barframe, text='Save', style='Accent.TButton', command=savefile)
close = ttk.Button(barframe, text='Close', style='Accent.TButton', command=killer)
#fonts = ttk.Entry(barframe, width=10)
calculater = ttk.Button(barframe, text='Calculater', width=10, style='Accent.TButton')
calander = ttk.Button(barframe, text='Calander', width=10, style='Accent.TButton')
increase_size = ttk.Button(barframe, text='+', width=2, command=increase_font_size)
decrease_size = ttk.Button(barframe, text='-', width=2, command=decrease_font_size)
bfonts.grid(row=0, column=0)
open.grid(row=0, column=1)
save.grid(row=0, column=2)
close.grid(row=0, column=3)
#fonts.grid(row=0, column=1)
calculater.grid(row=0, column=4)
calander.grid(row=0, column=5)
increase_size.grid(row=0, column=6)
decrease_size.grid(row=0, column=7)
scroll = ttk.Scrollbar(root, orient='vertical')
scroll.pack(side=RIGHT, fill='y')
text=Text(root, font=("Georgia", x), yscrollcommand=scroll.set, bg='#292929', height=1, width=1)
scroll.config(command=text.yview)
text.pack(fill=BOTH, expand=1)
root.mainloop()
错误代码
Traceback (most recent call last):
File "C:UsersHeroAppDataLocalProgramsPythonPython310libtkinter__init__.py", line 1921, in __call__
return self.func(*args)
File "c:UsersHeroDocumentsVisual Studio codeMy projectMainFrame.py", line 82, in savefile
myfile = open(filename, "w+")
TypeError: 'Button' object is not callable
这就是整个脚本和整个错误的全部内容。我希望这能有所帮助。我只是觉得如果我把整个代码作为它真正大的lol 发布,它会妨碍我
测试后,问题是将预定义的python变量分配给打开按钮。您所在的位置:
open = ttk.Button(barframe, text='Open', style='Accent.TButton')
只需将其更改为:
openButton = ttk.Button(barframe, text='Open', style='Accent.TButton')
然后下游更改为:
openButton.grid(row=0, column=1)