首页 > 解决方案 > 已解决 Python3,类和 tkinter 的困难

问题描述

  1. 项目清单

首先要开始,-我不擅长编程,-我很难理解大多数基础知识,-我总是尽我所能自己解决这样的问题。

我正在尝试创建一个制作 json 文件的简单 gui。一切正常。在我能够创建文件的意义上很好。现在我想清理我的代码并提升到一个新的水平。我已经在 tkinter 屏幕上添加了标签,这就是麻烦的开始。因为当我在不同的选项卡上时,该功能不会获取当前选定的项目,所以我添加了按钮来保存该列表,然后移动到不同的选项卡。

我有一个函数(Save_List_t),它查看列表框(a_lsb1)中的选定项目并将它们保存到列表(choice_list_t)中。当我按下按钮(a_button)时,此功能运行。

这样做之后我遇到了一个问题,我不想使用“全局”,但我需要其他函数(Mitre_Gen_Techs)中的列表来生成文件。当我按下第三个选项卡上的按钮时,此功能运行。(c.button1)

为了解决这个问题,我看到一个帖子有人使用一个类来修复它。然而,即使在阅读了关于类的文档之后,我仍然没有真正理解它。

现在我被卡住并得到错误。我觉得这并不奇怪,对我来说为什么它会给出错误是有道理的,但是我做错了什么或者我该如何解决这个问题。

错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\thans\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
TypeError: Save_List_t() missing 1 required positional argument: 'self'

我写的代码:

from tkinter import *
from attackcti import attack_client
from mitretemplategen import *
from tkinter import ttk


# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Mitre ATT&Ck
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
ac = attack_client()
groups = ac.get_groups()
groups = ac.remove_revoked(groups)
techs = ac.get_enterprise_techniques()
techs = ac.remove_revoked(techs)


# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Tkinter screen setup
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
root = Tk()

root.title("Mitre Att&ck")
root.minsize(900, 800)
root.wm_iconbitmap('')


# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Functions / classes
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

class Screen(object):
    def __init__(self):
        self.choice_list_t = []
        self.choice_list_g = []


    def Save_List_t(self):
        for item in a_lsb2.curselection():
            self.choice_list_t.append(a_lsb2.get(item))
        print(self.choice_list_t)


    def Save_List_g(self):
        choice_list_g = []
        for item in b_lsb1.curselection():
            self.choice_list_g.append(b_lsb1.get(item))
        print(self.choice_list_g)


    def Mitre_Gen_Techs(self):
        # Gen the json file
        # mitre_gen_techs(self.choice_list_t, techs)

        #testing class
        print(self.choice_list_t)


    def Mitre_Gen_Groups(self):
        # Gen the json file
        # mitre_gen_groups(self.choice_list_g, groups)

        #testing class
        print(self.choice_list_g)



def main():
    # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    # First Tkinter tab
    # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    rows = 0
    while rows < 50:
        root.rowconfigure(rows, weight=1)
        root.columnconfigure(rows, weight=1)
        rows += 1

    # Notebook creating tabs
    nb = ttk.Notebook(root)
    nb.grid(row=1, column=0, columnspan=50, rowspan=50, sticky='NESW')

    # Create the differend tabs on the notebook
    tab_one = Frame(nb)
    nb.add(tab_one, text='APG')

    tab_two = Frame(nb)
    nb.add(tab_two, text='Actors')

    tab_gen = Frame(nb)
    nb.add(tab_gen, text='test')


    # =-=- First Tab -=-=
    # List box 1
    a_lsb1 = Listbox(tab_one, height=30, width=30, selectmode=MULTIPLE)
    # List with techs
    a_lsb2 = Listbox(tab_one, height=30, width=30, selectmode=MULTIPLE)
    for t in techs:
        a_lsb2.insert(END, t['name'])


    # Save list, to later use in Screen.Mitre_Gen_Techs
    a_button = Button(tab_one, text="Save selected", command=Screen.Save_List_t)


    # =-=- Second Tab -=-=
    # List with TA's
    b_lsb1 = Listbox(tab_two, height=30, width=30, selectmode=MULTIPLE)
    for g in groups:
        b_lsb1.insert(END, g['name'])

    # Save list, to later use in Screen.Mitre_Gen_Groups
    b_button = Button(tab_two, text="Save selected", command=Screen.Save_List_g)


    # =-=- Third tab -=-=
    c_button = Button(tab_gen, text="Print group json", command=Screen.Mitre_Gen_Groups)
    c_button1 = Button(tab_gen, text="Print techs json", command=Screen.Mitre_Gen_Techs)


    # Placing the items on the grid
    a_lsb1.grid(row=1, column=1)
    a_lsb2.grid(row=1, column=2)

    b_lsb1.grid(row=1, column=1)

    a_button.grid(row=2, column=1)
    b_button.grid(row=2, column=1)

    c_button.grid(row=2, column=1)
    c_button1.grid(row=2, column=2)


    root.mainloop()

# If main file then run: main()
if __name__ == "__main__":
    main()

应用: 图像

标签: python-3.xtkinter

解决方案


我找到了一个解释出了什么问题的人。归功于 Scriptman ( ^ , ^ ) /

只需添加: sc = Screen()

并改变: Button(tab_apg, text="Save selected", command=sc.Save_List_t)

解决了这个问题。


推荐阅读