Python Forum
tkinter GUI, problem running seperate files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter GUI, problem running seperate files
#16
Okay let us say we wrote a Class with all its imports it needs such as this

from PyQt5.Widgets import QWidget, QLabel

class Exterior(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        self.lblTest = QLabel(Test)
***************************************************
Okay that is our class (called Test.py) outside our main program (called Main.py) we are writing now to import we need to do a few things (note the sub-directory is not necessary but it makes things cleaner)

Create a subfolder within your Main area call it say Utils so we have the following folder/file structure:
Main
    Utils
        Test.py
    Main.py
Now we have our Main program and our Test class that we want to import but just importing it will not work but why? Well because python requires this interesting little additive which we implement by creating an empty text file and renaming it to "__init__.py" then placing one within our Main folder and one within our Utils folder as follows:
Main
    Utils
        __init__.py
        Test.py
    __init__.py
    Main.py
Now within Main.py we can add the following code and import Test into Main

import Utils.Exterior as Ext
And reference its contents (with using the above import) like this:
    Ext.lblTest.setText('My Test')
Now that above bit of code will appear to do nothing if implemented as designed since we never show lblTest anywhere but that will set the value you just wont be able to see it. Still what the above does do is show you how to set up and import an exterior class into your current code base.
Reply


Messages In This Thread
RE: tkinter GUI, problem running seperate files - by Denni - Sep-27-2019, 02:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Help running a loop inside a tkinter frame Konstantin23 3 1,791 Aug-10-2023, 11:41 AM
Last Post: Konstantin23
  Why is the program not running? Is there a logical or syntax problem? behi00 10 2,347 Apr-01-2023, 12:50 AM
Last Post: woooee
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,199 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  Python3 tkinter radiobutton problem Nick_tkinter 14 6,403 Feb-15-2021, 11:01 PM
Last Post: Nick_tkinter
  tkinter python button position problem Nick_tkinter 3 3,709 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  [Tkinter] ClockIn/Out tkinter problem Maryan 2 2,326 Oct-12-2020, 03:42 AM
Last Post: joe_momma
  tkinter| listbox.insert problem Maryan 3 3,682 Sep-29-2020, 05:34 PM
Last Post: Yoriz
  Tkinter problem DPaul 6 4,336 May-28-2020, 03:40 PM
Last Post: DPaul
  [Tkinter] Tkinter - I have problem after import varaible or function from aGUI to script johnjh 2 2,682 Apr-17-2020, 08:12 PM
Last Post: johnjh
  [Tkinter] Problem with tkinter when creating .exe file Jan_97 2 4,730 Feb-27-2020, 05:17 PM
Last Post: Jan_97

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020