首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python3,对类和tkinter有困难

Python3,对类和tkinter有困难
EN

Stack Overflow用户
提问于 2020-06-02 11:00:04
回答 1查看 55关注 0票数 0

首先要把它启动,

  • 我不擅长编程,
  • 我很难理解大部分的基本知识,
  • 我总是尽我最大的努力亲自解决这类问题。

我正在尝试创建一个简单的gui来生成json文件。一切都很好。从我能创建文件的角度来说很好。现在,我想要清理我的代码,并将其提升到更高的水平。我在tkinter屏幕上添加了选项卡,这就是故障开始的地方。因为当我在一个不同的选项卡上时,函数没有得到当前选定的项,所以我添加了按钮来保存这个列表,然后移到不同的选项卡。

我有一个函数(Save_List_t),它从列表框(A_lsb1)中查看选定的项,并将它们保存到一个列表(Choice_list_t)中。当我按下按钮(A_button)时,这个函数就会运行。

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

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

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

错误:

代码语言:javascript
复制
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'

我写的代码:

代码语言:javascript
复制
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()

应用程序:图像

EN

回答 1

Stack Overflow用户

发布于 2020-06-03 09:06:07

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

简单地添加: sc = Screen()

以及更改:按钮(tab_apg,text=“selected",command=sc.Save_List_t)

解决了这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62150497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档