Tkinter-如何将变量分配给列表框中当前选定的项目? [英] Tkinter - How to assign variable to currently selected item in Listbox?

查看:43
本文介绍了Tkinter-如何将变量分配给列表框中当前选定的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个Python3.3 Tkinter可滚动列表框的帮助,该列表框会遍历所有用户安装的字体.此功能的目的是在程序的另一部分中更改文本字段中的字体....

I need help with a Python3.3 Tkinter scrollable list box that iterates through all the users installed fonts. The purpose of this function is to change the fonts in my Textfield in another part of my program....

from tkinter import *
import tkinter.font

def fontValue():
    fontroot=Tk()
    fontroot.wm_title('FONTS')

    fonts=list(tkinter.font.families())
    fonts.sort()

    fontbox = Listbox(fontroot,height=20)
    fontbox.pack(fill=BOTH, expand=YES, side=LEFT)

    scroll = Scrollbar(fontroot)
    scroll.pack(side=RIGHT, fill=Y, expand=NO)

    scroll.configure(command=fontbox.yview)
    fontbox.configure(yscrollcommand=scroll.set)



    for item in fonts:
        fontbox.insert(END, item)

    fontroot.mainloop()

那么如何将列表框中当前选择的字体字符串分配给变量?我想将当前选定的字体分配给一个变量....让我们称它为MainFontVar .....我没有在代码中放入变量,因为我不知道如何访问当前选定的字体....任何帮助将不胜感激..我为我的迟钝深表歉意.

So how do I assign the currently selected font string in my Listbox to a variable?? I want to assign the currently selected font to a variable.... lets call it the MainFontVar.....I didnt put the variable in this code, cuz I have no idea how to access the currently selected font....any help would be greatly appreciated....and I apologize for my retardation.

推荐答案

您需要保存字体列表,因为小部件只能为您提供选定的索引.大致情况:

You need to hold a list of your fonts as the widget can only give you the selected index. Something along the lines:

from tkinter import *
import tkinter.font

class Main(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)  

        self.fonts = list(tkinter.font.families())
        self.fonts.sort()

        self.list = Listbox(self)
        for item in self.fonts:
            self.list.insert(END, item)
        self.list.pack(side=LEFT, expand=YES, fill=BOTH)
        self.list.bind("<<ListboxSelect>>", self.PrintSelected)

        self.scroll = Scrollbar(self)
        self.scroll.pack(side=RIGHT, fill=Y)

        self.scroll.configure(command=self.list.yview)
        self.list.configure(yscrollcommand=self.scroll.set)

    def PrintSelected(self, e):
        print(self.fonts[int(self.list.curselection()[0])])

root = Main()
root.mainloop()

一个很棒的Tk教程位于 http://www.tkdocs.com/

A great Tk tutorial is located at http://www.tkdocs.com/

要获得更好的外观(在我的情况下,在Windows上),您可以对 Scrollbar 使用 ttk ,并在 Listbox中禁用激活元素的下划线(没有主题变体).

To get better look and feel (on Windows in my case), you can use ttk for Scrollbar and disable underline for activated element in Listbox (which does not have themed variant).

from tkinter import ttk
from tkinter import *
import tkinter.font

class Main(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)  

        self.fonts = list(tkinter.font.families())
        self.fonts.sort()

        self.list = Listbox(self, activestyle=NONE)
        for item in self.fonts:
            self.list.insert(END, item)
        self.list.pack(side=LEFT, expand=YES, fill=BOTH)
        self.list.bind("<<ListboxSelect>>", self.PrintSelected)

        self.scroll = ttk.Scrollbar(self)
        self.scroll.pack(side=RIGHT, fill=Y)

        self.scroll.configure(command=self.list.yview)
        self.list.configure(yscrollcommand=self.scroll.set)

    def PrintSelected(self, e):
        print(self.fonts[int(self.list.curselection()[0])])

root = Main()
root.mainloop()

这篇关于Tkinter-如何将变量分配给列表框中当前选定的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆