다른 클래스의 한 클래스에서 함수 호출

YusufChowdhury

내 코드를 게시하기 전에 이것이 전부는 아니지만 내 문제와 관련이 있다고 생각합니다. 사용자가 버튼을 클릭하면 첫 번째 클래스가 실행되므로 클래스 내용 (프레임)이 표시됩니다. 내 프레임 처리기는 다음과 같습니다.

class Begin(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        # Creating the initial frame
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (LoginScreen, RegisterWindow, RevisionTopics, dataRep, reviseTen, FrameTwo):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
            page_name = LoginScreen.__name__
            self.frames[page_name] = frame

        self.show_frame(LoginScreen) # Shows the page currently being interacted

자, 이것은 두 번째 프레임에서 실행해야하는 중요한 기능인 '시작'이있는 프레임입니다.

첫 번째 프레임 :

class reviseTen(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.startButton = tk.Button(self, text="Click here to start revision session", command = self.start)
        self.optionOne = tk.Button(self, text="Option One")
        self.optionTwo = tk.Button(self, text="Option Two")
        self.optionThree = tk.Button(self, text="Option Three")
        self.optionFour = tk.Button(self, text="Option Four")
        self.proceedButton = tk.Button(self, text="Proceed to next question", command=lambda: controller.show_frame(FrameTwo))
        self.question = tk.Label(self, text="What is the definition of: ")
        self.startButton.grid(row=0, column=0)

    def start(self): #This is what I wanna use in my second frame
        firstTime = True
        while firstTime:
            self.startButton.destroy()
            firstTime = False
        words = self.makeDict()

        initialListOne = ['Integer', 'Natural number', 'Rational numbers', 'Irrational numbers', 'Real numbers', 'Ordinal numbers', 'Binary', 'Hexadecimal']
        listOne = []
        for i in initialListOne:
            listOne.append(words[i])

        initialListTwo = ['Denary to Hex', 'Binary to Hex', 'ASCII', 'Unicode', 'Overflow error', 'Twos complement', 'Bitmapped graphics', 'Resolution']
        listTwo = []
        for i in initialListTwo:
            listTwo.append(words[i])

        initialListThree = [ 'Bit Colour Depth', 'Metadata', 'Sample resolution', 'Sample Rate', 'Audio file size', 'Nyquist Theorem', 'MIDI', 'Lossy Compression']
        listThree = []
        for i in initialListThree:
            listThree.append(words[i])

        initialListFour = ['Lossless Compression', 'Run Length Encoding', 'Dictionary compression', 'Encryption', 'Encryption steps', 'Caesar cipher',
                       'Brute force attack', 'Frequency analysis', 'Vernam cipher', 'One-Time Pad']
        listFour = []
        for i in initialListFour:
            listFour.append(words[i])

        listOfKeys = []  # Holds the keywords
        listOfValues = []  # Holds the definitions

        for key in words:
            listOfKeys.append(key)
            listOfValues.append(words[key])

        keywordPosition = random.randint(1, len(listOfKeys)-1)
        QKeyword = listOfKeys[keywordPosition]
        QDef = listOfValues[keywordPosition]


        self.question.grid(row=0, column=0)
        self.optionOne.grid(row=1, column=0)
        self.optionTwo.grid(row=2, column=0)
        self.optionThree.grid(row=3, column=0)
        self.optionFour.grid(row=4, column=0)
        self.proceedButton.grid(row=5, column=0)

        self.question.config(text=("What is the definition of: "+ QKeyword))

        randomOne = random.randint(0, len(listOne))
        randomTwo = random.randint(0, len(listTwo))
        randomThree = random.randint(0, len(listThree))
        randomFour = random.randint(0, len(listFour))

        selectButton = random.randint(1,4)
        if selectButton == 1:
            self.optionOne.config(text=QDef)
            self.optionTwo.config(text=listOfValues[randomTwo])
            self.optionThree.config(text=listOfValues[randomThree])
            self.optionFour.config(text=listOfValues[randomFour])
        elif selectButton == 2:
            self.optionOne.config(text=listOfValues[randomOne])
            self.optionTwo.config(text=QDef)
            self.optionThree.config(text=listOfValues[randomThree])
            self.optionFour.config(text=listOfValues[randomFour])
        elif selectButton == 3:
            self.optionOne.config(text=listOfValues[randomOne])
            self.optionTwo.config(text=listOfValues[randomTwo])
            self.optionThree.config(text=QDef)
            self.optionFour.config(text=listOfValues[randomFour])
        elif selectButton == 4:
            self.optionOne.config(text=listOfValues[randomOne])
            self.optionTwo.config(text=listOfValues[randomTwo])
            self.optionThree.config(text=listOfValues[randomThree])
            self.optionFour.config(text=QDef)

    def makeDict(self):
        dict = {}
        con = sql.connect("dataRep.db")
        cur = con.cursor()
        for column in cur.execute("SELECT keyword, definition FROM words"):
            variable = column[0]
            variable2 = column[1]
            dict[variable] = variable2
        return dict

두 번째 프레임 :

class FrameTwo(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.optionOne = tk.Button(self, text="Option One")
        self.optionTwo = tk.Button(self, text="Option Two")
        self.optionThree = tk.Button(self, text="Option Three")
        self.optionFour = tk.Button(self, text="Option Four")
        self.question = tk.Label(self, text="What is the definition of: ")

    # TRIED THIS - screen stays blank (but start method has code that makes the widgets appear
        self.start(controller)

    def start(self, controller):
        self.reviseTen = reviseTen(self, controller)

'reviseTen'프레임에서했던 것과 똑같은 기능을 완료하기 시작해야합니다. 기능은 실행 중이지만 두 번째 프레임에는 아무것도하지 않습니다. 그냥 비어 있습니다. 요소의 위치를 ​​지정하는 코드 (표시되도록 표시됨)는 시작 실행 후 실행됩니다.

내가 부르는 방식과 관련이 있습니까?

도움을 주셔서 감사합니다.

줄리앙

reviseTen대신 클래스에서 상속 하고 다음을 사용 tk.Frame하여 함수를 호출하십시오 super.

class FrameTwo(reviseTen):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.optionOne = tk.Button(self, text="Option One")
        self.optionTwo = tk.Button(self, text="Option Two")
        self.optionThree = tk.Button(self, text="Option Three")
        self.optionFour = tk.Button(self, text="Option Four")
        self.question = tk.Label(self, text="What is the definition of: ")
        super(FrameTwo, self).start(controller)

자세한 내용 super이 질문에 대한 답변을 확인하십시오 .

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

한 클래스의 멤버 함수를 다른 클래스에서 호출

분류에서Dev

다른 클래스의 한 클래스에서 파이썬 함수 호출

분류에서Dev

한 클래스에서 다른 클래스의 함수를 호출하는 방법은 무엇입니까?

분류에서Dev

한 클래스 함수에서 다른 클래스 함수로 변수를 호출하는 방법

분류에서Dev

작업이 수행 될 때 다른 클래스에서 한 클래스 호출

분류에서Dev

PHP-다른 클래스에서 클래스 함수 호출

분류에서Dev

자바의 다른 클래스에서 메인 클래스의 함수 호출

분류에서Dev

다른 클래스의 함수 호출

분류에서Dev

현재 클래스의 루프에서 다른 클래스 함수 호출

분류에서Dev

다른 클래스 PHP에서 클래스 내부의 함수 호출

분류에서Dev

Kivy의 다른 클래스에서 함수 호출

분류에서Dev

Swift의 다른 클래스에서 함수 호출

분류에서Dev

Android Studio의 다른 클래스에서 함수 호출

분류에서Dev

es6의 다른 클래스에서 함수 호출

분류에서Dev

coffeescript 클래스-다른 클래스에서 한 클래스 메서드 호출

분류에서Dev

다른 클래스의 한 클래스에서 메서드 호출

분류에서Dev

다른 클래스의 한 클래스에서 메서드 호출

분류에서Dev

call_user_func_array를 사용하여 동일한 클래스의 다른 함수에서 PHP 클래스 함수 호출

분류에서Dev

동일한 클래스 내에서 다른 함수의 출력 사용

분류에서Dev

다른 함수 내에서 클래스 함수 호출

분류에서Dev

extjs4의 다른 클래스 B 함수에서 클래스 A의 함수를 호출하는 방법

분류에서Dev

다른 클래스의 함수에 대한 스레드 간의 신호 전달?

분류에서Dev

클래스의 다른 함수에서 함수를 호출하는 Python

분류에서Dev

PHP 같은 클래스의 다른 함수에서 함수 호출

분류에서Dev

한 공용 클래스에서 다른 클래스로 정적 매개 변수 호출

분류에서Dev

다른 파일에서 클래스 및 함수를 호출하여 클래스의 변수 편집

분류에서Dev

클래스 내에서 다른 함수 호출

분류에서Dev

MATLAB : 다른 클래스에서 OOP 호출 함수

분류에서Dev

다른 클래스에서 함수를 호출하는 방법

Related 관련 기사

  1. 1

    한 클래스의 멤버 함수를 다른 클래스에서 호출

  2. 2

    다른 클래스의 한 클래스에서 파이썬 함수 호출

  3. 3

    한 클래스에서 다른 클래스의 함수를 호출하는 방법은 무엇입니까?

  4. 4

    한 클래스 함수에서 다른 클래스 함수로 변수를 호출하는 방법

  5. 5

    작업이 수행 될 때 다른 클래스에서 한 클래스 호출

  6. 6

    PHP-다른 클래스에서 클래스 함수 호출

  7. 7

    자바의 다른 클래스에서 메인 클래스의 함수 호출

  8. 8

    다른 클래스의 함수 호출

  9. 9

    현재 클래스의 루프에서 다른 클래스 함수 호출

  10. 10

    다른 클래스 PHP에서 클래스 내부의 함수 호출

  11. 11

    Kivy의 다른 클래스에서 함수 호출

  12. 12

    Swift의 다른 클래스에서 함수 호출

  13. 13

    Android Studio의 다른 클래스에서 함수 호출

  14. 14

    es6의 다른 클래스에서 함수 호출

  15. 15

    coffeescript 클래스-다른 클래스에서 한 클래스 메서드 호출

  16. 16

    다른 클래스의 한 클래스에서 메서드 호출

  17. 17

    다른 클래스의 한 클래스에서 메서드 호출

  18. 18

    call_user_func_array를 사용하여 동일한 클래스의 다른 함수에서 PHP 클래스 함수 호출

  19. 19

    동일한 클래스 내에서 다른 함수의 출력 사용

  20. 20

    다른 함수 내에서 클래스 함수 호출

  21. 21

    extjs4의 다른 클래스 B 함수에서 클래스 A의 함수를 호출하는 방법

  22. 22

    다른 클래스의 함수에 대한 스레드 간의 신호 전달?

  23. 23

    클래스의 다른 함수에서 함수를 호출하는 Python

  24. 24

    PHP 같은 클래스의 다른 함수에서 함수 호출

  25. 25

    한 공용 클래스에서 다른 클래스로 정적 매개 변수 호출

  26. 26

    다른 파일에서 클래스 및 함수를 호출하여 클래스의 변수 편집

  27. 27

    클래스 내에서 다른 함수 호출

  28. 28

    MATLAB : 다른 클래스에서 OOP 호출 함수

  29. 29

    다른 클래스에서 함수를 호출하는 방법

뜨겁다태그

보관