I'm trying to do a script in tkinter. Good to know: I'm kind of new to python.
The script takes user input to find the user input on a server. First time I run the script it works fine, but when trying to find something new the script gives me the error: AttributeError: 'str' object has no attribute 'get' So im guesseing that the user input/Button needs to be reset some how. And I donĀ“t understand why it works the first time and not the second time.
I've tried to find a good way to do this, but I have failed. Please halp a newbie.
import requests, re, urllib.request
import tkinter as tk
from tkinter import *
from tkinter import ttk
window = Tk()
window.title("Find answer")
ttk.Label(window, text="What you wanna find: ").pack()
stuff = ttk.Entry(window)
stuff.pack()
frame = Frame(window, width=200, height=50)
frame.pack()
servers = ["192.168.8.3", "192.68.8.2"]
def find():
global stuff
stuff = stuff.get()
stuff = stuff.lower()
for server in servers:
f = urllib.request.urlopen("http://"+server+"/find")
result = f.read().decode('utf-8')
lab = tk.Label(frame,text="server")
lab.pack()
print(server)
def clicked_start():
find()
start_btn = ttk.Button(text="Find the stuff", command=clicked_start)
start_btn.pack(fill="none")
window.mainloop()
i guess the reason is that you redefine stuff
global stuff
stuff = stuff.get()
stuff = stuff.lower()
the variable is originally an Entry Object, you redefine it as a string
Try to use another variable, eg
global stuff
stuffcontent = stuff.get()
stuffcontent = stuffcontent.lower()
Related
I'm new to coding, so I apologize if I'm making a really dumb mistake.
I have one module (INPUT_GUI.py) that opens a window where my user can input information, then hit a submit button.
I have another module (Error_Check.py) that I want to use to look over the inputs for errors. If there is an error, it throws up a message box "You dun goofed bud" which they can close and correct their input error (INPUT_GUI frame hasn't closed yet). The Error_Check.py should run each time the submit button is clicked.
I want to call both of these modules from Input_Master.py, which I want to close the INPUT_GUI frame if there were no errors.
Right now, when I run the Input_Master, I don't get anything. I've got a print command in there to show me the values it grabs from the Input GUI, but I just see a 0.
What am I doing wrong?
GUI_INPUT.py:
from tkinter import *
from tkinter import ttk
Input_Table = 0
def retrieve(): #returns the input values as a dictionary
global Input_Table
Input_Table={1:[inputA1.get(),inputB1.get()],2:[inputA2.get(),inputB2.get()]....}
return(Input_Table)
def Close_Input():
root.destroy()
def Input_Table_Gen():
root=Tk()
frame1=Frame(root)
frame1.pack(fill=x)
frame2=Frame(root)
frame2.pack(fill=x)
#...
frame11=Frame(root)
frame11.pack(fill=x)
inputA1=Entry(frame1,width=20)
inputA1.pack(side=LEFT,padx=5,pady=5)
inputB1=Entry(frame1,width=20)
inputB1.pack(side=LEFT,padx=5,pady=5)
#...
inputA10=Entry(frame1,width=20)
inputA10.pack(side=LEFT,padx=5,pady=5)
inputB10=Entry(frame1,width=20)
inputB10.pack(side=LEFT,padx=5,pady=5)
Submit_Button = Button(frame11,text="Submit",command=retrieve)
Submit_Button.pack()
root.mainloop()
Error_Check.py:
From GUI_INPUT import retrieve
x = {}
def Input_Errors(x)
#checks for errors, returns True if errors are found, and False if no errors are found
Input_Master.py:
import GUI_INPUT
from Error_Check import Input_Errors
GUI_INPUT.Input_Table_Gen
Error_Output = Error_Check.Input_Errors(Input_Table)
if Error_Output = True:
messagebox.showinfo("ERROR","You dun goofed, try again plz")
else:
GUI_INPUT.Close_Input
I am currently trying to turn a Python script I use regularly into an application by using Platypus. However, my script prompts the user for input several times and uses that input to construct a URL that is being used to make API requests. Here is an example of how this is used in my script:
member_id = raw_input("What member id will you be using? ")
The data taken from the user (and stored as a variable) is then used like this:
url_member = "https://api.example.com/member?member_id="+str(member_id)
Since the application created using Platypus won't allow for user input (based on the way I am requesting it through my script) I was going to try and use Tkinter as well. However, I have read through the documentation and am confused when it comes to the syntax (I am still new to Python in general).
Can anyone help, or show an example of how I can change my request for user input (based on my example above) using Tkinter so the application will work?
I am also using Python 2.7.
You can use the Entry() widget to get the user input as a variable.
A user can type in there ID and then hit the submit button. This button can be tied to a function that will do anything you need it to form there.
import tkinter as tk # Python 3 import
# import Tkinter as tk # Python 2 import
root = tk.Tk()
def my_function():
current_id = my_entry.get()
url_member = "https://api.example.com/member?member_id="+str(current_id)
print(url_member)
#do stuff with url_member
my_label = tk.Label(root, text = "Member ID# ")
my_label.grid(row = 0, column = 0)
my_entry = tk.Entry(root)
my_entry.grid(row = 0, column = 1)
my_button = tk.Button(root, text = "Submit", command = my_function)
my_button.grid(row = 1, column = 1)
root.mainloop()
I am trying to change a program's behaviour on the fly by editing its source file (under Python 3.4.2, Windows 8.1). My program is a tkinter GUI with a button that sums two values, but I want to be able to change the button's behaviour. I am currently trying to do this by editing the source file (changing, say, the addition to subtraction), saving it, and then clicking a button whose callback function imports the source file. I want my code changes to be reflected in the running GUI without having to exit and restart the program. I also want this import to only recompile the lines that I changed, rather than the entire file.
The program, reload0.py:
import time
import serial
import sys
import os
import tkinter as tk
from tkinter import ttk
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
try:
import Tkinter # Python 2
import ttk
except ImportError:
import tkinter as Tkinter # Python 3
import tkinter.ttk as ttk
mGui = Tk()
mGui.title("GUI")
mGui.geometry('400x200+100+100')
def mOp():
num1 = value1.get()
num2 = value2.get()
Op=num1+num2
name1.set('Sum')
name2.set(Op)
def mReLoad():
import reload0.py
mGui.update()
def mCheck():
if len(name1.get()) == 0:
name1.set('name1')
mGui.update()
if (len(name2.get()) == 0):
name2.set('name2')
mGui.update()
try:
print(value1.get())
except ValueError:
value1.set(0)
mGui.update()
try:
print(value2.get())
except ValueError as ValE:
value2.set(0)
mGui.update()
print(ValE)
value1 = DoubleVar()
value2 = DoubleVar()
name1 = StringVar()
name2 = StringVar()
mButtonSave = Button(mGui, text = "Operation", command = mOp, fg = 'Red').place(x=150,y=80)
mButtonLoad = Button(mGui, text = "ReLoad Operation", command = mReLoad, fg = 'Red').place(x=150,y=110)
mButtonLoad = Button(mGui, text = "Check", command = mCheck, fg = 'Red').place(x=150,y=140)
tText1 = Entry(mGui, textvariable = name1).place(x=10,y=80)
tText2 = Entry(mGui, textvariable = name2).place(x=10,y=100)
vText1 = Entry(mGui, textvariable = value1).place(x=10,y=120)
vText2 = Entry(mGui, textvariable = value2).place(x=10,y=140)
For your purpose of changing button functionality there are easy ways than changing source code, and as commented it's not possible.
judging from another question i saw of yours you are quite new to python programming. I would recommend spending some time on some basic tutorials getting to know python and some programming concepts first.
for instance
import reload.py
should simply be
import reload
https://wiki.python.org/moin/BeginnersGuide/NonProgrammers
read a book, do some examples, and so enough you will be the one answering the questions here, good luck!
I'm trying to make a launcher for my Python program with Tkinter. I used the execfile function, and fortunately it opened the target GUI. However, none of the buttons would work, and it would say the global variable most functions reference isn't defined.
The code to launch the program:
def launch():
execfile("gui.py")
That works. The base code for the target program:
from Tkinter import *
gui = Tk()
gui.title("This is a GUI")
EDIT:
Example of a button:
def buttonWin():
buttonWindow = Toplevel(gui)
button = Button(buttonWindow, text = "Button", width = 10, command = None)
button.pack()
When it references that 'gui' variable for Toplevel, it comes up with an error. I tried defining the 'gui' variable in the Launcher script, but that only caused the target script to open first, instead of the Launcher:
gui = Tk()
launcher = Tk()
launcher.title("Launcher")
def launch():
return execfile("gui.py")
launchButton = Button(launcher, text = "Launch", width = 10, command = launch)
When I try pressing one of this program's buttons, I get a NameError:
$NameError: Global variable 'gui' is not defined$
Also this is in Python 2.7.5.
Thank you anyone who answers, and sorry for any errors with the code blocks; I'm new.
The problem is that you have structured the Tkinter program incorrectly.
In "gui.py" you should have something like:
from Tkinter import *
gui= Tk()
gui.mainloop()
You can add buttons to perform functions and customize it:
from Tkinter import *
gui = Tk()
gui.title("This is a GUI")
def launch():
execfile("gui.py")
launchbutton = Button(gui, text='Launch Program', command=launch)
launchbutton.pack()
gui.mainloop()
I think with your function buttonWin you were trying to do what is normally handled by a class; see unutbu's answer here.
I'm not sure if I've addressed your problem, but this should be a start.
I'm following the tutorial found here on pages 31 and 32 http://www.ittc.ku.edu/~niehaus/classes/448-s04/448-standard/tkinter-intro.pdf .
I get two windows, one with OK and Cancel buttons and two entryboxes, and another that is blank. When I click OK or Cancel, that window disappears but the other blank window freezes up and I can't even close out. The only way to close it is by closing out of command prompt.
I'm getting the following error when I run it.
first = string.atoi(self.e1.get())
NameError: global name 'string' is not defined
I adjusted dialog2.py as shown in my comments. tkSimpleDialog.py is not changed at all (page 31 of the link above)
# File: dialog2.py
import tkSimpleDialog #added this
import os #added this
from Tkinter import * #added this
class MyDialog(tkSimpleDialog.Dialog):
def body(self, master):
Label(master, text="First:").grid(row=0)
Label(master, text="Second:").grid(row=1)
self.e1 = Entry(master)
self.e2 = Entry(master)
self.e1.grid(row=0, column=1)
self.e2.grid(row=1, column=1)
return self.e1 # initial focus
def apply(self):
first = string.atoi(self.e1.get())
second = string.atoi(self.e2.get())
print first, second # or something
root = Tk() #added this
d = MyDialog(root) #added this
You need to import the string module.
Although a better way to do this (without needing to import string) is to use the int builtin. i.e. change it to:
first = int(self.e1.get())
etc.
I'm guessing the reference manual you're working through was created for a very old version of python ...