簡體   English   中英

Tkinter Entry小部件中的可見textvariable

[英]Visible textvariable in tkinter Entry widget

我在tkinter中使用Entry Box小部件時遇到麻煩。 當用戶選擇Edit-> Backbone ...時,我想打開一個新窗口。 在此窗口中,將有許多Entry窗口小部件(為簡單起見,下面僅對其進行編碼)顯示了存儲在各種實例中的默認Strings元素類。 用戶應該能夠編輯此字符串並通過單擊“確定”保存它,並通過單擊“默認”將其返回到其默認值。 每次重新打開主干編輯器時,輸入框應始終顯示變量的當前值(如果重新啟動整個程序,則無需記住用戶輸入)。

打開“主干編輯器”窗口后,“輸入”框應顯示字符串文本變量,但無法顯示它。

from tkinter import *
from tkinter import ttk

class View(ttk.Frame):
    """Main GUI class"""

    def __init__(self, master = None):

        self.WIDTH = 450
        self.HEIGHT = 500

        self.lib = MolecularLibrary()

        # Set up the main window
        ttk.Frame.__init__(self, master, borderwidth=5, width=self.WIDTH, height=self.WIDTH)
        self.master.resizable(FALSE, FALSE)
        self.grid(column=0, row=0, sticky=(N, S, E, W))
        self.columnconfigure(0, weight=1)

        self.create_menus()

    def create_menus(self):
        """Produces the menu layout for the main window"""

        self.master.option_add('*tearOff', FALSE)

        self.menubar = Menu(self.master)
        self.master['menu'] = self.menubar

        # Menu Variables
        menu_edit = Menu(self.menubar)

        # Add the menus to the menubar and assign their variables
        self.menubar.add_cascade(menu=menu_edit, label = "Edit")

        ### Edit ###
        menu_edit.add_command(label="Backbone...", command=lambda : self.edit_backbone())


    def edit_backbone(self):
        """Shows a window where the backbone constituents can be edited"""
        backbone_window = Toplevel(borderwidth = 5)
        backbone_window.title("Backbone Editor")
        backbone_window.resizable(FALSE, FALSE)

        print("sugar value", self.lib.sugar_var)

        # LABELS FOR BACKBONE #
        # Phosphate annotations and input
        sugar_label = ttk.Label(backbone_window, text = "Sugar")

        #inputs
        sugar = ttk.Entry(backbone_window, textvariable = self.lib.sugar_var, justify = 'center', width=10)

        ### Buttons ###
        default = ttk.Button(backbone_window, text = "Defaults", command=lambda : defaults())
        okay = ttk.Button(backbone_window, text = "Okay", command=lambda : okay())
        cancel = ttk.Button(backbone_window, text = "Cancel", command=lambda : backbone_window.destroy())

        #content.grid(column=0, row=0)
        sugar_label.grid(column=2, row=1)
        sugar.grid(column=1, row=2, columnspan=3)

        default.grid(column=0, row=12, columnspan=3, pady=2)
        okay.grid(column=6, row=12, columnspan=3, pady=2)
        cancel.grid(column=9, row=12, columnspan=4, pady=2)

        backbone_window.focus()

        def defaults():
            """Reset the backbone and bases to their defaults."""

            self.lib.set_molecules()


        def okay():
            """Set the backbone and base variables to the user set values."""

            self.lib.sugar_var.new_formula(sugar.get())

            backbone_window.destroy()


class MolecularLibrary:
    """
    """

    def __init__(self, atom_file = r'C:\MyPyProgs\OSeq\resources\ATOMS.txt',
                 precision = 4):

        self.molecules = {}
        self.atom_file = atom_file
#         self.molecule_file = molecule_file

        # Variables
        self.set_molecules()


    def set_molecules(self):
        """
        Set all of the molecules for the backbone and bases to their default values and forumlae.
        """
                ### Sugar ###
        self.sugar_var = Element('C5H8O3', 'A')


    def add_molecule(self, molecule):
        """(MolecularLibrary, list) -> None
           Returns a dictionary of the molecule name as an Element
           {molecule[0]: Element}
        """
        print(molecule)
        tmp = self.get_mass(molecule[1])
        return {molecule[0]: Element(molecule[1], molecule[0], tmp[0], tmp[0])}

class Element:
    """
    Creates an element with the following construct:
    [symbol, name, monoisotopic, average]
    """

    def __init__(self, symbol, name):

        self.symbol = symbol
        self.name = name

    def __str__(self):

        return str([self.symbol, self.name])

    def get_name(self):
        """Returns the name of the Element"""

        return self.name

    def get_symbol(self):
        """Returns the symbol of the Element"""

        return self.symbol

    def new_formula(self, new_formula):
        """replace the formula with new_formaula and recalculate the 
           average and monoisotopic masses."""

        self.symbol = new_formula


if __name__ == "__main__":
    root = Tk()
    root.title("Sequencer")
    view = View(root)
    root.mainloop()

上面的代碼是我程序的重剝離版本,但是具有相同的基本體系結構。 恐怕仍然有很多代碼,我通常會嘗試將其分解得更多,但是我不確定我遇到的問題是否來自體系結構。

為了使用textvariable屬性,必須給它一個textvariable變量的實例: StringVarIntVarBooleanVarDoubleVar

在這里了解更多有關這些變量的好起點: http : //effbot.org/tkinterbook/variable.htm

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM