-4

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
cyber-security47
  • 31
  • 1
  • 1
  • 6
  • 2
    I get the feeling that you're missing something important about functions: they can take arguments. – Makoto Jun 19 '17 at 21:59

2 Answers2

1

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

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
0

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.

Easton Bornemeier
  • 1,918
  • 8
  • 22
  • This didn't work I've read the post you sent and tried couple of times with absolute failure.... Anyone else out there can help? I really appreciate everyone's who helped and will help in future. – cyber-security47 Jun 19 '17 at 23:20
  • A return statement on a function called from a button is useless. Where do you think the value is being returned to? Also, you can't add an argument to a function definition unless you also pass an argument to that function when it's being called. Your code example doesn't show how to do that. – Bryan Oakley Jun 20 '17 at 12:03