-1

I am building my first larger Python application using tkinter, python 3.6.3 and window OS. The GUI for the application consists of a Notebook with several tabs. Each of the tabs in turn contains a Labelframe which in turn contains a number of other widgets.

When searching Stackflow, I found the idea to lets each labelFrame be a class. Thereafter import the class in the main.py and finally creating an instance of the class.

Now when pressing the button 'Start' in tab1 I would like to execute the 'printThis' function. Ideally I would like to use the function defined in the script main.py. It would also be interested in knowing how to call the 'printThis' method withing the Run_Test_Window class. Unfortunately I have not solved either problem.

Interestingly the program actually prints "Now successful" without that I do anything but when I press the 'Start'-button nothing happens.

Grateful for help! Thanks!

main.py

import tkinter as tk  
import tkinter.ttk as ttk

import RunTestClass as RT

def printThis():
  print('Successful')

root    = tk.Tk()
note    = ttk.Notebook(root)
note.grid()  

tab1    = ttk.Label(note, width = -20)
note.add(tab1, text = "       Run Test      ")

window1 = RT.Run_Test_Window(tab1)

root.mainloop()

RunTestClass.py

import tkinter as tk
import tkinter.ttk as ttk

# from main import printThis

class Run_Test_Window:

    def printThis(self):
        print('Now successful!') 

    def __init__(self,tab1):
        runTestLabelFrame = ttk.LabelFrame(text ="Run Test", padding =10)
        runTestLabelFrame.grid(in_ = tab1, padx = 20, pady = 20)

        self.startButton  = ttk.Button(runTestLabelFrame,    text="START",command=self.printThis())
        self.startButton.grid(row=5, column=1, padx = 10)
user2423970
  • 9
  • 1
  • 2
  • your title has nothing to do with your problem, also you never specified where you want to call printThis – AntiMatterDynamite Jan 29 '18 at 12:57
  • convert this line window1 = RT.Run_Test_Window(tab1) to this RT(tab1) for initiation. – Ozan Jan 29 '18 at 13:01
  • Sorry AntiMatterDynamite. I thought I have described it by writing "when pressing the button 'Start' in tab1 I would like to execute the 'printThis' function". Probably I should have used the word 'call' instead of 'execute' – user2423970 Jan 29 '18 at 15:11

1 Answers1

0

If I'm correct you want the button to use the printThis() from main. This can be done by adding the following in your main:

rt = RT.Run_Test_Window(tab1)
rt.startButton.configure(command=printThis)

To call the printThis() defined in RunTestClass, use (also in main)

rt.printThis()

Note: leave the brackets when creating the button in the command argument. So change it to this:

self.startButton  = ttk.Button(runTestLabelFrame,    text="START",command=self.printThis)
ViG
  • 1,848
  • 1
  • 11
  • 13
  • Thanks ViG! rt.startButton.configure(command=printThis) works perfectly fine for me. rt.printThis() also work but also prints 'Now successful!' once although I have not press the Startbutton – user2423970 Jan 29 '18 at 15:06
  • @user2423970 It will print it once because of the `rt.printThis()`. If you comment that, it no longer prints `Now successfull`; and will only print `Successfull` when the button is pressed. – ViG Jan 29 '18 at 15:22