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

76 views Asked by At

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
2

There are 2 answers

0
toyota Supra On BEST ANSWER

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

2
Easton Bornemeier On

Add return website_name to the end of your proceed3() function

Add an argument website_name to your OpenChrome() function.

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)
    return website_name


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

I would suggest reading this tutorial regarding how to work with functions in python, as this will be fundamental to furthering your programming efforts.