Cannot call show_frame function in tkinter outise of its class - python

With the code below (that I took from sentdex) I am trying to raise PageOne window when the correct password("123") is inserted in the first page. However, I get the error: TypeError: show_frame() missing 1 required positional argument: 'cont'. Isn't PageOne the argument ? Why is not working ? Could you also please explain what "controller" variable does ?
Thank you very much in advance.
import tkinter as tk
import tkinter as tk
from PIL import Image,ImageTk
LARGE_FONT= ("Verdana", 12)
class Root(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
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 (StartPage, PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
self.entry=tk.Entry(self, width = 35)
self.entry.insert(0, 'Enter password')
self.entry.config(fg = "grey")
# entry.bind('<FocusIn>', self.EntryFieldClicked)
# entry.bind("<Return>", (lambda event: self.SubmitPass()))
self.entry.place(relx=.5, rely=.3,anchor = tk.CENTER)
button=tk.Button( self,text="Show Password", width = 20,command = self.ShowPass).place(relx=.5, rely=.4,anchor = tk.CENTER)
button=tk.Button(self,text="Submit",width = 20, command=self.SubmitPass).place(relx=.5, rely=.5,anchor = tk.CENTER)
self.entry.config(fg = "grey")
self.entry.bind('<FocusIn>', self.EntryFieldClicked)
self.entry.bind("<Return>", (lambda event: self.SubmitPass ))
label=tk.Label(self,text="Log in to continue")
label.pack()
button = tk.Button(self, text="Visit Page 1",
command=lambda: controller.show_frame(PageOne))
button.pack()
def EntryFieldClicked(self,event):
if self.entry.get() == 'Enter password':
self.entry.delete(0, tk.END)
self.entry.insert(0, '')
self.entry.config(fg = 'black', show = "*")
def ShowPass(self):
self.entry.config(fg = 'black', show = "")
def SubmitPass(self):
global Password
Password = self.entry.get()
if Password == "123":
Root.show_frame(PageOne)
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page One!!!", font=LARGE_FONT)
label.pack(pady=10,padx=10)
self.controller = controller
button1 = tk.Button(self, text="Back to Home",
command=lambda: self.controller.show_frame(StartPage))
button1.pack()
button2 = tk.Button(self, text="Page Two",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page Two!!!", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = tk.Button(self, text="Page One",
command=lambda: controller.show_frame(PageOne))
button2.pack()
app = Root()
app.mainloop()
Blockquote

The problem is this line:
Root.show_frame(PageOne)
You are calling the method on the class rather than the instance, so the first argument is passes to the self parameter.
Your class needs to keep a reference to the controller, so that you can call the method via the controller.
class StartPage(tk.Frame):
def __init__(self, parent, controller):
self.controller = controller
...
def SubmitPass(self):
global Password
Password = self.entry.get()
if Password == "123":
self.controller.show_frame(PageOne)

Related

How can I make the tkinter window fixed?

This is my current code, and I am struggling to understand where I need to put additional code to make sure that my tkinter window is fixed. Code taken from: https://pythonprogramming.net/change-show-new-frame-tkinter/
import tkinter as tk
LARGE_FONT= ("Verdana", 12)
class SeaofBTCapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = False)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="NC's Ice-Cream Shop", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button = tk.Button(self, text=">>",
command=lambda: controller.show_frame(PageOne))
button.pack()
#button2 = tk.Button(self, text="Visit Page 2",
# command=lambda: controller.show_frame(PageTwo))
#button2.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Order Details", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="<<",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = tk.Button(self, text=">>",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Receipt", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="<<",
command=lambda: controller.show_frame(PageOne))
button1.pack()
button2 = tk.Button(self, text="New",
command=lambda: controller.show_frame(StartPage))
button2.pack()
app = SeaofBTCapp()
app.mainloop()
As mentioned in the comments, you simply need to set your root window resizable method to false. In your case it is the SeaofBTCapp class.
For Example:
import tkinter as tk
LARGE_FONT= ("Verdana", 12)
class SeaofBTCapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.resizable(False,False) # <------------------- added this
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = False)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="NC's Ice-Cream Shop", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button = tk.Button(self, text=">>",
command=lambda: controller.show_frame(PageOne))
button.pack()
#button2 = tk.Button(self, text="Visit Page 2",
# command=lambda: controller.show_frame(PageTwo))
#button2.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Order Details", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="<<",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = tk.Button(self, text=">>",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Receipt", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="<<",
command=lambda: controller.show_frame(PageOne))
button1.pack()
button2 = tk.Button(self, text="New",
command=lambda: controller.show_frame(StartPage))
button2.pack()
app = SeaofBTCapp()
app.mainloop()

Updating or "Refreshing" frames in Tkinter

This basically creates all the frames and brings the one we call to the front right?
So as some variables change i want the frames to change accordingly, i tried a few refreshing functions i found while searching in google but none of them worked in this code.
oooooooooooooooooooooooooooooo
Code:
import tkinter as tk
from tkinter import *
from tkinter import ttk
from functools import partial
class MovieTicketapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.geometry(self, "500x400")
container = tk.Frame(self)
container.pack(side='top', fill='both', expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
global user
self.frames = {}
for F in (StartPage, SignUp, Login, EditMovie):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky='nsew')
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
global user
button_signup = ttk.Button(self, text='Sign Up',
command=lambda: controller.show_frame(SignUp)).pack(pady=10, padx=15)
button_EditMovie = ttk.Button(self, text='Edit Movie Details',
command=lambda: controller.show_frame(EditMovie))
if user == "admin":
button_EditMovie.pack(pady=10, padx=15)
class SignUp(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button_login = ttk.Button(self, text='Login',
command=lambda: controller.show_frame(Login)).pack(pady=10, padx=15)
button_home = tk.Button(self, text='Home',
command=lambda: StartPage.__init__()).pack(pady=10, padx=15)
class Login(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
def validateLogin(email, password):
e = email.get()
p = password.get()
global user
user = e
StartPage.update(self)
controller.show_frame(StartPage)
self.pack_forget()
emailLabel = tk.Label(self, text='Email').pack()
email = tk.StringVar()
emailEntry = tk.Entry(self, textvariable = email).pack()
passwordLabel = tk.Label(self,text="Password").pack()
password = tk.StringVar()
passwordEntry = tk.Entry(self, textvariable=password, show='*').pack()
validateLogin = partial(validateLogin, email, password)
loginButton = tk.Button(self, text="Login", command= validateLogin).pack(pady=15, padx=15)
button_home = tk.Button(self, text='Home',
command=lambda: controller.show_frame(StartPage)).pack(pady=10, padx=15)
class EditMovie(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button_home = tk.Button(self, text='Home',
command=lambda: controller.show_frame(StartPage))
button_home.pack(pady=10, padx=10)
app = MovieTicketapp()
app.mainloop()
......

how to open a new window from menu widget in tkinter

i'm trying to develop a simple Gui that display different pages when the button in the menu is clicked and i have been able to make other buttons work but the menu is giving me errors.
just like other apps that from the menu person can navicate the app.
when i run the code and click on newtest in file menu, i get this error
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/home/pi/Documents/py script/helium1/test.py", line 34, in <lambda>
command=lambda: controller.show_frame(PageOne))
AttributeError: 'Frame' object has no attribute 'show_frame'
here is the code
import tkinter as tk
from tkinter import *
import datetime
import time
import paho.mqtt.client as mqtt
LARGE_FONT= ("Verdana", 12)
def messageFunction (client, userdata, message):
topic = str(message.topic)
message = str(message.payload.decode("utf-8"))
print(topic + " " + message)
HeliumClient = mqtt.Client("xokaxvwt")
HeliumClient.username_pw_set(username = "xokaxvwt", password="MGlEiIwOHM9-")
HeliumClient.connect("m16.cloudmqtt.com", 15998)
HeliumClient.subscribe("Switch_1")
HeliumClient.subscribe("Switch_2")
HeliumClient.on_message = messageFunction
HeliumClient.loop_start()
class MenuBar(tk.Menu):
def __init__(self, parent, controller):
tk.Menu.__init__(self, controller)
self.controller = controller
fileMenu = tk.Menu(self, tearoff=0)
self.add_cascade(label="File", underline=0, menu=fileMenu)
fileMenu.add_command(label="New Test",
command=lambda: controller.show_frame(PageOne))
fileMenu.add_separator()
fileMenu.add_command(label="Exit")
class Helium(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
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 (StartPage, PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
self.controller = controller
self.menubar = MenuBar(self, parent)
self.controller.config(menu=self.menubar)
label = tk.Label(self, text="Start Page", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button = tk.Button(self, text="Visit Page 1", command=lambda: controller.show_frame(PageOne))
button.pack()
button2 = tk.Button(self, text="Visit Page 2", command=lambda: controller.show_frame(PageTwo))
button2.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page One!!!", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="Back to Home", command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = tk.Button(self, text="Page Two", command=lambda: controller.show_frame(PageTwo))
button2.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, con):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page Two!!!", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="Back to Home", command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = tk.Button(self, text="Page One", command=lambda: controller.show_frame(PageOne))
button2.pack()
app = Helium()
#creating Date/time
def display_time():
Date = datetime.datetime.now()
Date_Label['text'] = Date
app.after(1000, display_time)
Date_Frame = LabelFrame(app, text= "Date/time")
Date_Frame.place(x=790, y=5, height=35, width=200)
Date_Label = Label(Date_Frame)
Date_Label.grid(column=0, row=0)
app.geometry('1000x450')
app.title("Helium")
app.resizable(False, False)
display_time()
app.mainloop()
You are passing parent argument to MenuBar but it's expecting a controller. You also need to pass the parent to the superclass, not controller. You need to create MenuBar like this:
class MenuBar(tk.Menu):
def __init__(self, parent, controller):
tk.Menu.__init__(self, parent)
...
class StartPage(tk.Frame):
def __init__(self, parent, controller):
...
self.menubar = MenuBar(self)
...

Passing variables between different classes and definition in tkinter

I have a problem with my code. I am unable to pass a variable to another class once a submit button is pressed in my tkinter frame.
I have followed the advice from a post already (How to access variables from different classes in tkinter?), which has helped but I still have issues.
From where to where I need these variables is commented on the code below:
import tkinter as tk
from tkinter import StringVar
LARGE_FONT = ("Verdana", 12)
class Club(tk.Tk):
def get_page(self, page_class):
return self.frames[page_class]
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
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.shared_data = {
"username": tk.StringVar(),
"password": tk.StringVar(),
}
self.frames = {}
for F in (Terminal, newUser, newUserSubmitButton, delUser):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(Terminal)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class Terminal(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Welcome to the club terminal. Click the options below", font=LARGE_FONT)
label.grid(columnspan=3)
button = tk.Button(self, text="Visit new User",
command=lambda: controller.show_frame(newUser))
button.grid(row=1, column=0)
button2 = tk.Button(self, text="Visit del User",
command=lambda: controller.show_frame(delUser))
button2.grid(row=1, column=1)
class newUser(tk.Frame):
def __init__(self, parent, controller):
def submitButton():
username = self.controller.shared_data["username"].get()
print(username)
controller.show_frame(newUserSubmitButton)
##username variable from here to...
tk.Frame.__init__(self, parent)
welcomelabel = tk.Label(self, text="Add New User/s", font=LARGE_FONT)
welcomelabel.grid(columnspan=2, sticky="ew")
userNameLabel = tk.Label(self, text="Username")
userNameLabel.grid(row=1, column=0, sticky="e")
userNameEntry = tk.Entry(self, textvariable=self.controller.shared_data["username"])
userNameEntry.grid(row=1, column=1)
userMemTypeLabel = tk.Label(self, text="Membership Type")
userMemTypeLabel.grid(row=2, column=0, sticky="e")
variable = StringVar(self)
variable.set("Full")
userMemTypeMenu = tk.OptionMenu(self, variable, "Full", "Half")
userMemTypeMenu.grid(row=2, column=1)
userMemYearsLabel = tk.Label(self, text="Years that member is in the club")
userMemYearsLabel.grid(row=3, column=0, sticky="e")
userMemYearsEntry = tk.Entry(self)
userMemYearsEntry.grid(row=3, column=1)
self.controller = controller
newusersubmitbutton = tk.Button(self, text="submit", command=submitButton)
newusersubmitbutton.grid(columnspan=2)
class newUserSubmitButton(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
##username variable goes here
page1 = self.controller.get_page(newUser.submitButton)
page1.username.set("Hello, world")
print(page1)
class delUser(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="del User!!!", font=LARGE_FONT)
label.pack(pady=10, padx=10)
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(Terminal))
button1.pack()
button2 = tk.Button(self, text="new User",
command=lambda: controller.show_frame(newUser))
button2.pack()
app = Club()
app.title("Club Terminal")
app.iconbitmap("table.ico")
app.mainloop()
Whenever I run this code, I get an AttributeError: 'newUser' object has no attribute 'controller'.
Any help is greatly appreciated, I'll be more than happy to try any ideas out.
With regards.
There are more problems in this code, but to solve that one, add the line:
self.controller=controller
To the newUser classes __init__ function.

Starting another script using a button in tkinter

I am trying to run another script within a script using a button in tkinter
I have tried two methods one being
import os
class SeaofBTCapp(tk.Tk):
#initilization
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Battery Life Calculator")
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 (StartPage, PageOne, PageTwo, PageThree):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
#raise each page to the front
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
def helloCallBack():
os.system('python test2.py')
#the start page
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = ttk.Label(self, text="Start Page", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button = ttk.Button(self, text="Visit Page 1",command=lambda:controller.show_frame(PageOne))
button.pack()
button1 = ttk.Button(self, text="Visit Page 2",command=lambda:controller.show_frame(PageTwo))
button1.pack()
button2 = ttk.Button(self, text="Visit Graph page",command=lambda:controller.show_frame(PageThree))
button2.pack()
button3 = ttk.Button(self, text="execute test",command=lambda:controller.helloCallBack)
button3.pack()
No errors are given but when I hit execute nothing happens
The other method i tried was to
import test2
but it runs the script automatically and it prints "i am a test"
I should note the script i am trying to call is simply a test and just prints "i am a test"
any help is appreciated!
thanks
****edit****
import tkinter as tk
from tkinter import ttk
import test2
LARGE_FONT= ("Verdana", 12)
class SeaofBTCapp(tk.Tk):
#initilization
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Battery Life Calculator")
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 (StartPage, PageOne, PageTwo, PageThree):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
#raise each page to the front
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
#the start page
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = ttk.Label(self, text="Start Page", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button = ttk.Button(self, text="Visit Page 1",command=lambda:controller.show_frame(PageOne))
button.pack()
button1 = ttk.Button(self, text="Visit Page 2",command=lambda:controller.show_frame(PageTwo))
button1.pack()
button2 = ttk.Button(self, text="Visit Graph page",command=lambda:controller.show_frame(PageThree))
button2.pack()
button3 = ttk.Button(self, text="execute test",command=test2.main)
button3.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = ttk.Label(self, text="Page One", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = ttk.Button(self, text="Back to start page",command=lambda:controller.show_frame(StartPage))
button1.pack()
button1 = ttk.Button(self, text="Visit Page 2",command=lambda:controller.show_frame(PageTwo))
button1.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = ttk.Label(self, text="Page One", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = ttk.Button(self, text="Back to Start Page",command=lambda:controller.show_frame(StartPage))
button1.pack()
button1 = ttk.Button(self, text="Back to Page One",command=lambda:controller.show_frame(PageOne))
button1.pack()
class PageThree(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = ttk.Label(self, text="Graph Page", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = ttk.Button(self, text="Back to Start Page",command=lambda:controller.show_frame(StartPage))
button1.pack()
app = SeaofBTCapp()
app.mainloop()
test2.p is as follows
def main():
if __name__ == "__main__":
print("Executing as main program")
print("Value of __name__ is: ", __name__)
main()
Your first method is not recommended for that. If you have another python script you should definitely import it.
About the second method:
My guess is that the script test2.py is written without
if __name__ == "__main__":
main()
And that's why it shoot you message when you import it.

Categories

Resources