Python 如何更改字体选项

Python 如何更改字体选项,python,tkinter,fonts,notepad,Python,Tkinter,Fonts,Notepad,我想用Python Tkinter制作一种记事本编辑器,它有一个菜单,我可以在其中编辑字体(字体的大小和样式) 不管我怎么做,我总是会出错。不过,我对Python还是新手,所以可能是我犯了一个明显的错误。我希望有人能帮忙 当我点击“Opmaak”(即布局)然后点击“Lettergrootte”(即字体大小)时,我希望弹出一个选项菜单,在那里我可以更改字体大小和样式,但我得到了以下错误: > Exception in Tkinter callback Traceback (most rece

我想用Python Tkinter制作一种记事本编辑器,它有一个菜单,我可以在其中编辑字体(字体的大小和样式)

不管我怎么做,我总是会出错。不过,我对Python还是新手,所以可能是我犯了一个明显的错误。我希望有人能帮忙

当我点击“Opmaak”(即布局)然后点击“Lettergrootte”(即字体大小)时,我希望弹出一个选项菜单,在那里我可以更改字体大小和样式,但我得到了以下错误:

> Exception in Tkinter callback
Traceback (most recent call last):
  File "AppData\Local\Mu\pkgs\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
TypeError: __opmaak() missing 1 required positional argument: 'nieuweFont'
这是我的密码:

import tkinter as tk 
import os  
from tkinter import * 
from tkinter.messagebox import *
from tkinter.filedialog import *

class Kladblok:
    __root = Tk()

    __thisBreedte = 300
    __thisHoogte = 300
    __thisTekstVlak = Text(__root) 
    __thisMenuBar = Menu(__root) 
    __thisBestandMenu = Menu(__thisMenuBar, tearoff=0) 
    __thisBewerkenMenu = Menu(__thisMenuBar, tearoff=0)
    __thisOpmaakMenu = Menu(__thisMenuBar, tearoff=0)
    __thisHelpMenu = Menu(__thisMenuBar, tearoff=0) 
    __thisScrollBar = Scrollbar(__thisTekstVlak)      
    __bestand = None

    def __init__(self,**kwargs): 

        try: 
            self.__root.wm_iconbitmap("Notepad.ico")  
        except: 
            pass

        try: 
            self.__thisBreedte = kwargs['breedte'] 
        except KeyError: 
            pass

        try: 
            self.__thisHoogte = kwargs['hoogte'] 
        except KeyError: 
            pass

        self.__root.title("Naamloos - Kladblok") 

        screenBreedte = self.__root.winfo_screenwidth() 
        screenHoogte = self.__root.winfo_screenheight() 

        links = (screenBreedte / 2) - (self.__thisBreedte / 2)  

        top = (screenHoogte / 2) - (self.__thisHoogte /2)  

        self.__root.geometry('%dx%d+%d+%d' % (self.__thisBreedte, self.__thisHoogte, links, top))  

        self.__root.grid_rowconfigure(0, weight=1) 
        self.__root.grid_columnconfigure(0, weight=1) 

        self.__thisTekstVlak.grid(sticky = N + E + S + W)

        self.__thisBestandMenu.add_command(label="Nieuw", command=self.__nieuwBestand) 

        self.__thisBestandMenu.add_command(label="Open", command=self.__openBestand) 

        self.__thisBestandMenu.add_command(label="Opslaan", command=self.__opslaanBestand)

        self.__thisBestandMenu.add_separator()                                          
        self.__thisBestandMenu.add_command(label="Exit", command=self.__exitBestand) 
        self.__thisMenuBar.add_cascade(label="Bestand", menu=self.__thisBestandMenu)

        self.__thisOpmaakMenu.add_command(label="Lettergrootte", command=self.__opmaak)

        self.__thisMenuBar.add_cascade(label="Opmaak", menu=self.__thisOpmaakMenu)

        self.__thisBewerkenMenu.add_command(label="Knippen", command=self.__knippen)              

        self.__thisBewerkenMenu.add_command(label="Kopieren", command=self.__kopieeren)          

        self.__thisBewerkenMenu.add_command(label="Plakken", command=self.__plakken)          

        self.__thisMenuBar.add_cascade(label="Bewerken", menu=self.__thisBewerkenMenu)    

        self.__thisHelpMenu.add_command(label="Over mij", command=self.__overMij)
        self.__thisMenuBar.add_cascade(label="Help mij", menu=self.__thisHelpMenu) 

        self.__root.config(menu=self.__thisMenuBar) 

        self.__thisScrollBar.pack(side=RIGHT,fill=Y)                     

        # Scrollbar will adjust automatically according to the content         
        self.__thisScrollBar.config(command=self.__thisTekstVlak.yview)      
        self.__thisTekstVlak.config(yscrollcommand=self.__thisScrollBar.set) 

    def __exitBestand(self): 
        self.__root.destroy()

    def __nieuwBestand(self): 
        self.__root.title("Naamloos - Kladblok") 
        self.__bestand = None
        self.__thisTekstVlak.delete(1.0,END) 

    def __openBestand(self): 

        self.__bestand = askopenfilename(defaultextension=".txt", filetypes=[("Alle bestanden","*.*"), ("Tekst bestanden","*.txt")]) 

        if self.__bestand == "": 
            self.__bestand = None

        else: 
            self.__root.title(os.path.basename(self.__bestand) + " - Kladblok") 
            self.__thisTekstVlak.delete(1.0,END) 

            bestand = open(self.__bestand,"r") 

            self.__thisTekstVlak.insert(1.0,bestand.read()) 

            bestand.close() 

    def __opslaanBestand(self):
        if self.__bestand == None: 
            self.__bestand = asksaveasfilename(initialfile='Naamloos.txt', defaultextension=".txt", filetypes=[("Alle bestanden","*.*"), ("Tekst bestanden","*.txt")]) 

            if self.__bestand == "": 
                self.__bestand = None

            else: 
                bestand = open(self.__bestand,"w") 
                bestand.write(self.__thisTekstVlak.get(1.0,END)) 
                bestand.close() 

                self.__root.title(os.path.basename(self.__bestand) + " - Kladblok") 

        else: 
            bestand = open(self.__bestand,"w") 
            bestand.write(self.__thisTekstVlak.get(1.0,END)) 
            bestand.close() 

    def __knippen(self): 
        self.__thisTekstVlak.event_generate("<<Cut>>") 

    def __kopieeren(self): 
        self.__thisTekstVlak.event_generate("<<Copy>>") 

    def __plakken(self): 
        self.__thisTekstVlak.event_generate("<<Paste>>")    

    def __overMij(self):
        showinfo("Kladblok", "Copyright Timbo") 

    def __nieuweFont(self, *args):
        nieuweFont = (font.get(), fontSize.get())
        open['font'] = nieuweFont
        return nieuweFont

    def __opmaak(self, nieuweFont):
        font = StringVar(self.__root)
        font.set("Times")
        font.trace("w", nieuweFont)
        fontOptions = OptionMenu(self.__root, font, "Arial", "Times", "Helvetica", "Comic")

    #start programma
    def start(self): 
        self.__root.mainloop()

kladblok = Kladblok(width=600,height=400) 
kladblok.start()
将tkinter作为tk导入
导入操作系统
从tkinter进口*
从tkinter.messagebox导入*
从tkinter.filedialog导入*
克拉德布洛克级:
__root=Tk()
__此风速=300
__这个hoogte=300
__thisTekstVlak=文本(uu根)
__thisMenuBar=菜单(\uuuu根)
__thisBestandMenu=菜单(\uuu thisMenuBar,tearoff=0)
__thisBewerkenMenu=菜单(uu thismmenubar,tearoff=0)
__thisOpmaakMenu=菜单(\uu thisMenuBar,tearoff=0)
__thisHelpMenu=菜单(\uuu thisMenuBar,tearoff=0)
__thisScrollBar=滚动条(thisTekstVlak)
__最佳和=无
定义初始(自我,**kwargs):
尝试:
self.\uuu root.wm\u iconbitmap(“Notepad.ico”)
除:
通过
尝试:
self.\u thisBreedte=kwargs['breedte']
除KeyError外:
通过
尝试:
self.u thisHoogte=kwargs['hoogte']
除KeyError外:
通过
self.\uuu root.title(“Naamloos-Kladblok”)
screenBreedte=self.\uuu root.winfo\u screenwidth()
screenHoogte=self.\uuu root.winfo\u屏幕高度()
链接=(screenBreedte/2)-(self.\u thisBreedte/2)
top=(屏幕hoogte/2)-(自身)
self.\u根.geometry(“%dx%d+%d+%d%”(self.\u thisBreedte,self.\u thisHoogte,links,top))
self.\uuuu root.grid\u rowconfigure(0,权重=1)
self.\uuuu root.grid\u column配置(0,权重=1)
自身网格(粘性=N+E+S+W)
self.\uuu thisbestand菜单。添加命令(label=“Nieuw”,command=self.\uuu nieusbestand)
self.\uu此最佳和菜单。添加命令(label=“Open”,command=self.\uu openBestand)
self.\uuu此最佳与菜单。添加命令(label=“Opslaan”,command=self.\uuu opslaanBestand)
self.\u此最佳和菜单。添加分隔符()
self.\u此最佳和菜单。添加命令(label=“Exit”,command=self.\u exitBestand)
self.\u此菜单栏。添加级联(label=“Bestand”,menu=self.\u此bestandmenu)
self.\uuu这个opmaakmenu.添加命令(label=“Lettergrootte”,command=self.\uu opmaak)
self.\uuu thismmenubar.add\u cascade(label=“Opmaak”,menu=self.\uuuu thismopmaakmenu)
self.\uuu这个bewerkenmenu.添加命令(label=“Knippen”,command=self.\uu Knippen)
self.\uu这个bewerkenmenu.添加命令(label=“Kopieren”,command=self.\uu kopieeren)
self.\uuu thisBewerkenMenu.添加命令(label=“Plakken”,command=self.\uu Plakken)
self.\uuu thisMenuBar.add\u级联(label=“Bewerken”,menu=self.\uuuu thisBewerkenMenu)
self.\u此帮助菜单。添加命令(label=“Over mij”,command=self.\u overMij)
self.\u此菜单栏。添加\u级联(label=“Help mij”,menu=self.\u此帮助菜单)
self.\uuuu root.config(菜单=self.\uuuu此菜单栏)
self.\u thisScrollBar.pack(侧=右,填充=Y)
#滚动条将根据内容自动调整
self.\uuuThisScrollbar.config(命令=self.\uuuuThisTeksTvlak.yview)
self.\uuu thisTekstVlak.config(yscrollcommand=self.\uuu thiscrollbar.set)
def _exitBestand(自身):
self.\uuu root.destroy()
def__nieuwBestand(自我):
self.\uuu root.title(“Naamloos-Kladblok”)
self.\uu bestand=无
删除(1.0,结束)
def _openBestand(自):
self.\uu bestand=askopenfilename(defaultextension=“.txt”,filetypes=[(“Alle bestanden”,“**”),(“Tekst bestanden”,“*.txt”))
如果self.\u bestand==“”:
self.\uu bestand=无
其他:
self.\uuu root.title(os.path.basename(self.\uuu bestand)+“-Kladblok”)
删除(1.0,结束)
贝斯特兰德=开放式(self.\u贝斯特兰德,“r”)
self.\u thisTekstVlak.insert(1.0,bestand.read())
bestand.close()
def _opslaanBestand(自我):
如果self.\uu bestand==无:
self.\uu bestand=asksaveasfilename(initialfile='Naamloos.txt',defaultextension=“.txt”,filetypes=[(“Alle bestanden”,“**”),(“Tekst bestanden”,“*.txt”))
如果self.\u bestand==“”:
self.\uu bestand=无
其他:
贝斯特兰德=开放式(self.\u贝斯特兰德,“w”)
编写(self.\u thisTekstVlak.get(1.0,结束))
bestand.close()
self.\uuu root.title(os.path.basename(self.\uuu bestand)+“-Kladblok”)
其他:
贝斯特兰德=开放式(self.\u贝斯特兰德,“w”)
编写(self.\u thisTekstVlak.get(1.0,结束))
bestand.close()
迪夫·克尼潘(自我):
self.\u thisTekstVlak.event\u generate(“”)
def__kopieeren(自我):
self.\u thisTekstVlak.event\u generate(“”)
德夫·普拉肯(自我):
self.\u thisTekstVlak.event\u generate(“”)
def__overMij(自我):
showinfo(“Kladblok”,“Timbo版权所有”)
def_u nieuwent(self,*args):
nieuweFont=(font.get(),fontSize.get())
打开['font']=nieuweFont
返回nieuweFont
def__opmaak(self,nieuwefort):
font=StringVar(自根)
字体设置(“时代”)
font.trace(“w”,nieuweFont)
fontOptions=OptionMenu(self.\uu根,字体,“Arial”,“Times”,“Helvetica”,“Comic”)
#启动程序
def启动(自):
self.\uuu root.mainloop()
kladblok=kladblok(宽=600,高=400)
kladblok.start()
如果您能帮我修改代码,我们将不胜感激。谢谢你的支持
      def __opmaak(self,nieuweFont)
            self.__thisOpmaakMenu.add_command(label="Lettergrootte", command=self.__opmaak)
           TypeError: __opmaak() missing 1 required positional argument: 'nieuweFont'
       self.__thisOpmaakMenu.add_command(label="Lettergrootte", command=lambda: self.__opmaak(nieuweFont))
from tkinter import * 

class Kladblok:

    def __init__(self, master): 
        self.font1 = font=("Open Sans Standard",16,"bold")
        self.font2 = font=("Times",12,"bold")
        self.master = master
        self.__thisTekstVlak = Text(self.master, font=self.font1) 
        self.__thisMenuBar = Menu(self.master) 
        self.__thisOpmaakMenu = Menu(self.__thisMenuBar, tearoff=0)
        self.__thisScrollBar = Scrollbar(self.__thisTekstVlak) 

        self.master.title("Naamloos - Kladblok")  
        self.master.geometry(("300x300"))  
        self.master.grid_rowconfigure(0, weight=1) 
        self.master.grid_columnconfigure(0, weight=1) 
        self.__thisTekstVlak.grid(sticky = N + E + S + W)

        self.__thisOpmaakMenu.add_command(label="Lettergrootte", command=lambda: self.__opmaak(self.font2))

        self.__thisMenuBar.add_cascade(label="Opmaak", menu=self.__thisOpmaakMenu)
        self.master.config(menu=self.__thisMenuBar) 

        self.__thisScrollBar.pack(side=RIGHT,fill=Y)                     

        # Scrollbar will adjust automatically according to the content         
        self.__thisScrollBar.config(command=self.__thisTekstVlak.yview)      
        self.__thisTekstVlak.config(yscrollcommand=self.__thisScrollBar.set) 

    def __opmaak(self, nieuweFont):
        self.__thisTekstVlak.configure(font=nieuweFont)

if __name__ == "__main__":
    root = Tk()
    kladblok = Kladblok(root) 
    root.mainloop()