python - Tkinter - 如何将变量分配给列表框中当前选定的项目?

标签 python listbox tkinter python-3.3

我需要有关 Python3.3 Tkinter 可滚动列表框的帮助,该列表框可迭代所有用户安装的字体。此函数的目的是更改程序另一部分的文本字段中的字体......

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......我没有将变量放入此代码中,因为我不知道如何访问当前选择的字体......任何帮助将不胜感激......并且我为我的迟钝道歉。

最佳答案

您需要保存字体列表,因为小部件只能为您提供选定的索引。大致思路是这样的:

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/

为了获得更好的外观和感觉(在我的例子中是在 Windows 上),您可以使用 ttk对于 Scrollbar并禁用 Listbox 中激活元素的下划线(没有主题变体)。

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()

关于python - Tkinter - 如何将变量分配给列表框中当前选定的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20274252/

相关文章:

python - 程序结束后如何删除按钮?

python - 如何将关键字参数传递给 sklearn 管道中的预测方法

python - 在 Python 中的 sock.sendall() 之后调用 sock.recv()

python - 单批处理中的 Tensorflow 可变图像大小

delphi - Delphi XE5 FireMonkey 上的 ListBox 项目限制

python - 相同高度的 Tkinter 列表框和复选框

python - 如何在 Django 模型中保存对象列表?

javascript - 设置列表框的滚动位置

python - 如何从 tkinter TreeView 中删除记录以应用 sqlite3 中的更改

Python tkinter Canvas 在启动期间获取可见区域