Reputation: 47
from Tkinter import *
import requests
import base64
var = "lol"
var2 = "lol"
var3 = "111111"
var4 = "31039888252154&31039888252154&0&X168&1411869345&IQSAXYOS&"+var2+"&"+var3+"&&1&3&1&[email protected]&goohoo&1411603529&&2&"
encoded = base64.b64encode(var4)
var5 = encoded
payload = {'X1': '218',
'X127': 'X325',
'X171':var,
'X677':'1420103258',
'X678':'208180484',
'X691':var5,
'X763':'1'}
def bluh(e):
var = e1.get()
var2 = e2.get()
var3 = e3.get()
def Send(e):
requests.post("http://us23.chatzy.com/", data=payload)
def p1(e):
print var
print var2
print var3
#Sh!t in the GUI
root = Tk()
root.title("Spam Project")
root.geometry("200x200")
app = Frame(root)
app.grid()
button1 = Button(app, text = "Send")
button1.grid()
button1.bind('<Button-1>', Send)
e1 = Entry(root)
e1.grid()
e1.insert(0,"Message")
e2 = Entry(root)
e2.grid()
e2.insert(0,"Name")
e3 = Entry(root)
e3.grid()
e3.insert(0,"Color in Hex")
button2 = Button(app, text = "Submit")
button2.grid()
button2.bind('<Button-1>', bluh)
button3 = Button(app, text = "Print")
button3.grid()
button3.bind('<Button-1>', p1)
var = e1.get()
var2 = e2.get()
var3 = e3.get()
#Event loop
root.mainloop()
Basically, what this is supposed to do is take a name, message and hex color and send them as an HTTP request to a chat, this all works in terminal but then I tried to stitch a GUI on top, at the very top, var
, var2
, var3
are all default values and I have the submit button to change those values, but it simply does not work, so I made the "print" button to print the three values and they do not change, can anyone tell me what's wrong with my code and how can I make the three entries change the variables according to what's inside them?
Here's what the variables do.
Upvotes: 0
Views: 110
Reputation: 55469
The usual way in GUI code to avoid using globals is to use a class. I hope this isn't too advanced for you. But if it is, well you gotta learn this stuff sometime if you want to do serious programming. :) So read about classes in the Python docs, and then if you still can't figure out what something in my code's doing, ask & I'll try to explain.
I've commented out the actual requests
stuff for testing purposes.
#! /usr/bin/env python
from Tkinter import *
#import requests
import base64
def build_payload(var1, var2, var3):
var4 = "31039888252154&31039888252154&0&X168&1411869345&IQSAXYOS&" \
+ var2 + "&" + var3 \
+ "&&1&3&1&[email protected]&goohoo&1411603529&&2&"
print 'var4 = [%s]\n' % var4
var5 = base64.b64encode(var4)
payload = {
'X1': '218',
'X127': 'X325',
'X171': var1,
'X677': '1420103258',
'X678': '208180484',
'X691': var5,
'X763': '1'
}
return payload
class my_GUI(object):
def __init__(self):
#Build GUI
root = Tk()
root.title("Spam Project")
root.geometry("200x200")
app = Frame(root)
app.grid()
button = Button(app, text = "Send")
button.grid()
button.bind('<Button-1>', self.send_data)
self.e1 = Entry(root)
self.e1.grid()
self.e1.insert(0, "Message")
self.e2 = Entry(root)
self.e2.grid()
self.e2.insert(0, "Name")
self.e3 = Entry(root)
self.e3.grid()
self.e3.insert(0, "Color in Hex")
button = Button(app, text = "Print")
button.grid()
button.bind('<Button-1>', self.print_data)
#Start tkinter event loop
root.mainloop()
def print_data(self, e):
var1 = self.e1.get()
var2 = self.e2.get()
var3 = self.e3.get()
print var1, var2, var3
def send_data(self,e):
var1 = self.e1.get()
var2 = self.e2.get()
var3 = self.e3.get()
print var1, var2, var3
payload = build_payload(var1, var2, var3)
print 'Sending payload', payload
#requests.post("http://us23.chatzy.com/", data=payload)
def main():
my_GUI()
if __name__ == '__main__':
main()
PS. I've changed the names of some of the functions from what they were in your code, but I've left the variables names (mostly) as they were. In future, try to use more meaningful names - it makes life easier both for yourself & for other people who are trying to read your code.
Upvotes: 2
Reputation: 35891
To use global variables in Python you need the global
statement. So, you need to modify your bluh
function like this:
def bluh(e):
global var, var2, var3
var = e1.get()
var2 = e2.get()
var3 = e3.get()
(I've also fixed the code to use e1
, e2
, e3
resp. instead of e1
only)
In your version you were creating new, local variables var
, var2
and var3
inside the bluh
function, having nothing to do with the global ones defined at the top of your file. Please also note that in general using global variables is discouraged.
The same goes to the p1
function, as you also want to access globals.
Upvotes: 1