tkinter에서 항목 상자 위젯을 사용하는 데 문제가 있습니다. 사용자가 편집 -> 백본 ...을 선택할 때 열 수있는 새 창이 필요합니다. 이 창에는 클래스 요소 인 여러 인스턴스에 저장된 기본 문자열을 보여주는 많은 항목 위젯 (단순화를 위해 아래에 코딩 된 하나의 항목)이 있습니다. 사용자는이 문자열을 편집하고 확인을 클릭하여 기본값을 클릭하여 기본값으로 되돌릴 수 있습니다. 입력 상자는 백본 편집기를 다시 열 때마다 항상 변수의 현재 값을 표시해야합니다 (전체 프로그램이 다시 시작되면 사용자 입력을 기억할 필요가 없습니다).tkinter에서 보이는 텍스트 변수 항목 위젯
'백본 편집기'창을 열면 항목 상자에 문자열 텍스트 변수가 표시되어야하지만이를 표시 할 수 없습니다.
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
속성을 사용하기 위해
이전에 시도했지만 동일한 기능의 항목 상자를 정의하기 직전에 할당 할 때 작동하지 않았습니다. Element 클래스가 StringVars로 String을 저장 한 곳에서 새로운 구현을 시도했으며 현재 작동합니다. Element 클래스에서 StringVars를 정의하는 것이 훨씬 더 좋은 방법입니다! 당신의 도움을 주셔서 대단히 감사합니다! – Primigenia