Search code examples
pythonpython-2.7python-3.xgae-python27

Seeking Help: python2.7: working on this function; want to get input from "def proceed3()" to "def openChrome()"


How do i pass my website_name which is a URL, https://www.google.com/ for instance; from "def proceed3()" to "def openChrome()" ? Any Suggestions?

from Tkinter import *


def proceed3():
    popup = Toplevel()
    popup.geometry("350x175+350+180")
    popup.resizable(width=False, height=False)
    instruction = Label(popup, text="Enter the URL and pressGO!").pack()
    website_name = Entry(popup, width=50).pack(pady=5)
    goButton = Button(popup, text="GO!", command=openChrome)
    goButton.pack(pady=5)


def openChrome():
    openWebsite = website_name.get()
    os.system(r"start chrome " + openWebsite)


windows = Tk()

windows.geometry("200x200+375+280")
windows.resizable(width=False, height=False)



submitButton = Button(windows, text='OpenChrome', command=proceed3)
submitButton.pack(pady=5)

windows.mainloop()

TRACEBACK ERROR:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1542, in __call__
return self.func(*args)
File "E:/educational_data/PyCharmProjects/web_grabber/starflow_grabber.py", 
line 15, in openChrome
openWebsite = website_name.get()
NameError: global name 'website_name' is not defined

Solution

  • want to get input from def proceed3() to def openChrome().

    The problem can be fixed.

    For Python 2.7, Use Tkinter.

    For Python 3.x, Use tkinter.

    Avoid wildcard import *. Use this import tkinter as tk then use prefix tk. for widgets.

    • Add namespace import os.
    • On line 8, move instruction outside of function and place windows.resizable.
    • On line 9, move website_name outside of function and place windows.resizable.
    • On tk.Entry, split into 2 lines. So we can pass the website_name to def openChrome()
    • Both tk Entry(popup,..) and tk.Label(popup,..) to both tk Entry(windows,..) and tk.Label(windows,..).

    Run the script.

    Snippet:

    import Tkinter as tk . #For Python 2.7
    import tkinter as tk. #For Python 3.x
    import os
    
    
    def proceed3():
        popup = tk.Toplevel()
        popup.geometry("350x175+350+180")
        popup.resizable(width=False, height=False)
         
        goButton = tk.Button(popup, text=" Press GO!", command=openChrome)
        goButton.pack(pady=5)
    
    
    def openChrome():
        openWebsite = website_name.get()
        os.system(r"start chrome " + openWebsite)
    
    
    windows = tk.Tk()
    
    windows.geometry("400x200")
    windows.resizable(width=False, height=False)
    
    instruction = tk.Label(windows, text="Enter the URL and press openChrome button!").pack()
    
    website_name = tk.Entry(windows, width=50)
    website_name.pack(pady=5)
    
    submitButton = tk.Button(windows, text='OpenChrome', command=proceed3)
    submitButton.pack(pady=5)
    
    windows.mainloop()
    

    Screenshot:

    enter image description here