亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 tkinter 中制作字體對話框?

如何在 tkinter 中制作字體對話框?

阿晨1998 2023-07-27 16:33:05
我需要幫助在 tkinter 中制作字體對話框。到目前為止,這是我的代碼:from tkinter import *root = Tk()root.geometry("600x600")def fontDialog():    root2 = Toplevel(root)    root2.geometry("300x300")    root2.mainloopbutton = Button(root, text="font dialog", command=fontDialog)root.mainloop所以在 def fontDialog 中,我做了一個屏幕。我不知道如何制作一個更改字體系列和大小的字體對話框。如果你愿意,請幫忙。
查看完整描述

1 回答

?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

字體選擇器的制作非常簡單。您真正要做的就是運行一個循環font.families()并將insert每次迭代返回到Listbox. 從那里,您只需告訴它將持久字體引用的 更改為單擊時family選擇的任何內容。對于將持久字體引用應用于其選項的任何內容,字體都會發生變化。ListboxListboxfont


import tkinter as tk

from tkinter import font



class App(tk.Tk):

    def __init__(self):

        tk.Tk.__init__(self)


        #persistent font reference

        textfont = font.Font(family='arial', size='14')

        

        #something to type in ~ uses the persistent font reference

        tk.Text(self, font=textfont).grid(row=0, column=0, sticky='nswe')

        

        #make the textfield fill all available space

        self.grid_rowconfigure(0, weight=1)

        self.grid_columnconfigure(0, weight=1)

        

        #font chooser

        fc = tk.Listbox(self)

        fc.grid(row=0, column=1, sticky='nswe')


        #insert all the fonts

        for f in font.families():

            fc.insert('end', f)


        #switch textfont family on release

        fc.bind('<ButtonRelease-1>', lambda e: textfont.config(family=fc.get(fc.curselection())))

        

        #scrollbar ~ you can actually just use the mousewheel to scroll

        vsb = tk.Scrollbar(self)

        vsb.grid(row=0, column=2, sticky='ns')

        

        #connect the scrollbar and font chooser

        fc.configure(yscrollcommand=vsb.set)

        vsb.configure(command=fc.yview)



if __name__ == "__main__":

    app = App()

    app.title('Font Chooser Example')

    app.geometry(f'800x600+200+200')

    app.mainloop()


查看完整回答
反對 回復 2023-07-27
  • 1 回答
  • 0 關注
  • 178 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號