Passing user entry to another script - python

I am trying to use a variable that I get from an entry field in tkinter to another script.
In short:
I want to use the user's input in an entry field in another script. This does not work at all.
Any help highly appreciated!
I tried so far for Script2:
from Script1 import App
test = App()
print(test.write_slogan(self))
TypeError: init() missing 1 required positional argument: 'master'
and
from Script1 import App
print(App.write_slogan())
write_slogan() missing 1 required positional argument: 'self'
and
from Script1 import App
print(App.write_slogan(self))
NameError: name 'self' is not defined
and
import Script1
print(Script1.App.a)
AttributeError: type object 'App' has no attribute 'a'
Script1:
from tkinter import *
class App:
a = 0
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.slogan = Button(frame,
text="Hello",
command=self.write_slogan)
self.slogan.pack(side=LEFT)
self.entry1 = Entry(root, width=15)
self.entry1.pack(side=LEFT)
self.importbutton = Button(frame,
text="import",
command=self.importing)
self.importbutton.pack(side=LEFT)
def write_slogan(self):
print ("Test!")
App.a = self.entry1.get()
print(App.a)
return App.a
def importing(self):
print('Import')
import Script2
if __name__ == '__main__':
root = Tk()
app = App(root)
root.mainloop()
Script2:
import Script1

You've since fixed this I believe, but I'm remarking the information for others that may find their way here: The self within a class function is the instance python will automatically pass when calling the defs. (Unless you're using classmethod or staticmethod for which are not useful in your use case and are a slightly more advanced topic)
# -- Script1 \/
class App:
def __init__(self, master):
# self here is automatically passed
# but you need to pass the "master" arg.
frame = Frame(master)
# ... Example:
self._value = 0
def set_value(self, val):
self._value = val
def get_value(self):
return self._value
# -- Use (probably in Scipt2)
master = Tk()
app = App(master)
print (app.get_value()) # Notice how we don't pass self
>>> 0
app.set_value("my new value") # This string is the "val" arg
print (app.get_value())
>>> my new value
Scipt1.App.a issue
The main issue you're having with is most likely to do with the way python manages modules. The class is writing to App.a in Script1.App but not Script2.Script1.App.a. This is expected behavior so I recommend instead trying to work with something like:
class App:
def __init__(self, master):
# ... Make your tk widgets as you already have
def set_entry_value(self, val):
self.entry1.set(val)
def get_entry_value(self):
self._last_recieved_entry_value = self.entry1.get()
# -- Script2 \/
# The if __name__ == '__main__' is not run on imported
# scripts, only the script python starts execution on
# ~$> python Script2.py
import Script1
if __name__ == '__main__':
root = Tk()
app = Script1.App(root)
# Possibly set a default?
app.set_entry_value("Default value")
root.mainloop()
# - Some time after the mainloop is over
my_lastest_value = root.get_entry_value()
This way, you're letting the local instance of objects handle their internal values. If you're looking to set a class member of an alternate module, then doing so in Script2 may work.
if __name__ == '__main__':
root = Tk()
app = Script1.App(root)
# Do something to set the entry
Script1.App.a = app.get_entry_value()
But be warned, that may not scale across multiple modules.

Related

How to solve Problem with Multiprocessing in Tkinter?

Over here I am using multiprocessing to run multiple algorithms in tkinter. At first I tried using threading, but it can't work properly in my program. Below is an idea of my program workflow, it works something like this, but just different functions:
from tkinter import *
from multiprocessing import Process
def SquarFunc(Square):
for i in range(1,1000):
Square.set(str(i**2))
def CubeFunc(Cube):
for i in range(1,1000):
Cube.set(str(i**3))
if __name__ == "__main__":
window= Tk()
Square= StringVar()
Cube= StringVar()
window.geometry("500x500")
A= Label(window, textvariable= Square)
A.place(x=200, y=200)
B= Label(window, textvariable= Cube)
B.place(x=300, y=300)
Squaring= Process(target=SquarFunc, args=(Square, ))
Cubing= Process(target=CubeFunc, args=(Cube, ))
Squaring.start()#Error originates here
Cubing.start()
Squaring.join()
Cubing.join()
window.mainloop()
The error produced is this:
TypeError: cannot pickle '_tkinter.tkapp' object
Anybody knows how to fix this?? thanks in advance!
Here is an example of how to communicate with other processes if using multiprocessing (explanation is in comments, time.sleep is used just for the example because otherwise those loops will complete in a few microseconds):
from tkinter import Tk, StringVar, Label
from multiprocessing import Process, Manager
import time
def square_func(d, name):
for i in range(1, 1000):
# update data in the shared dict
d[name] = i
time.sleep(0.1)
def cube_func(d, name):
for i in range(1, 1000):
# update data in the shared dict
d[name] = i
time.sleep(0.1)
def update_string_vars(d, *variables):
for var in variables:
# get the value from shared dict
value = d[str(var)]
if value is not None:
# set string var to the value
var.set(str(value))
# schedule this to run again
window.after(100, update_string_vars, d, *variables)
# cleanup process upon closing the window in case
# processes haven't finished
def terminate_processes(*processes):
for p in processes:
p.terminate()
if __name__ == "__main__":
window = Tk()
window.geometry("500x500")
# bind the terminator to closing the window
window.bind('<Destroy>', lambda _: terminate_processes(
square_process, cube_process))
square_var = StringVar()
cube_var = StringVar()
Label(window, text='Square:').pack()
Label(window, textvariable=square_var).pack()
Label(window, text='Cube:').pack()
Label(window, textvariable=cube_var).pack()
# create the manager to have a shared memory space
manager = Manager()
# shared dict with preset values as to not raise a KeyError
process_dict = manager.dict({str(square_var): None, str(cube_var): None})
square_process = Process(
target=square_func, args=(process_dict, str(square_var))
)
cube_process = Process(
target=cube_func, args=(process_dict, str(cube_var))
)
square_process.start()
cube_process.start()
# start the updater
update_string_vars(process_dict, square_var, cube_var)
window.mainloop()
Useful:
Sharing state between processes
shortly about tkinter and processes
See also:
I strongly advise against using wildcard (*) when importing something, You should either import what You need, e.g. from module import Class1, func_1, var_2 and so on or import the whole module: import module then You can also use an alias: import module as md or sth like that, the point is that don't import everything unless You actually know what You are doing; name clashes are the issue.
I strongly suggest following PEP 8 - Style Guide for Python Code. Function and variable names should be in snake_case, class names in CapitalCase. Don't have space around = if it is used as a part of keyword argument (func(arg='value')) but have space around = if it is used for assigning a value (variable = 'some value'). Have space around operators (+-/ etc.: value = x + y(except here value += x + y)). Have two blank lines around function and class declarations. Object method definitions have one blank line around them.

tkinter new class can't use .get()/float()

I have defined a new Entry subclass: NewEntry, but it can't get the numbers which are put in it. How can I fix this?
When I click the button, the error message is showed:
ValueError: could not convert string to float:
from Tkinter import *
root = Tk()
class NewEntry(Entry):
def __init__(self,parent,cusdef='1'): #Initiation default number is '1'
Entry.__init__(self,parent)
self.cusdef = cusdef
v=StringVar()
v.set(self.cusdef)
self = Entry(self,textvariable=v)
self.pack()
return
def GetNum():
a=e.get()
print float(a)
return
e = NewEntry(root)
e.pack(fill='x')
button = Button(root,command=GetNum)
button.pack(fill='x')
root.mainloop()
You seem to be trying to initialize your Entry subclass here:
self = Entry(self,textvariable=v)
self.pack()
But instead, you're merely overwriting the variable called self and creating a new Entry which gets discarded.
Instead you need to do the Entry.__init__ call once, with the correct arguments:
class NewEntry(Entry):
def __init__(self,parent,cusdef='1'):
self.cusdef = cusdef
v=StringVar()
v.set(self.cusdef)
Entry.__init__(self,parent, textvariable=v)
self.pack()
return

button command option in tkinter

In the little GUI app below. When I use button's command option to call a function. It doesn't work like this: self.update() rather it works like this: self.update. Why so? Is is some special way that command option of a button works? I think a method or a function should be called with those braces (), unless it's a property:
i.e.
#name.setter:
def setter(self, name):
self.name = name
#main
object.name = "New_obj"
Note: The above is just a template so you might get my point. I didn't write the complete valid code. Including class and everything.
from tkinter import *
class MuchMore(Frame):
def __init__(self, master):
super(MuchMore,self).__init__(master)
self.count =0
self.grid()
self.widgets()
def widgets(self):
self.bttn1 = Button(self, text = "OK")
self.bttn1.configure(text = "Total clicks: 0")
self.bttn1["command"] = self.update # This is what I am taking about
self.bttn1.grid()
def update(self):
self.count += 1
self.bttn1["text"] = "Total clicks" + str(self.count)
#main
root = Tk()
root.title("Much More")
root.geometry("324x454")
app = MuchMore(root)
It is a high order function, meaning you are referencing a function as an object. You are not calling the function and assigning the command to the return value of the function. See here for more information.
The command parameter takes a reference to a function -- ie: the name of the function. If you add parenthesis, you're asking python to execute the function and give the result of the function to the command parameter.

Python: Tkinter access variable from another module

Somewhere in my code I import this module:
import sModule as s
And initialize my main Tkinter window like this:
base = tk.Tk()
mw = MainWindow(base).grid()
s.parent = sys.modules[__name__]
base.mainloop()
And MainWindow class is something like this:
class MainWindow(tk.Frame):
def __init__(self, parent):
self.info1 = tk.StringVar()
self.info2 = tk.StringVar()
What I'm trying to do is accessing info1 and info2 in sModule like this:
parent.mw.info1.set(str1)
And I'm getting this error:
AttributeError: 'NoneType' object has no attribute 'info1'
Which part is wrong?
Replace following line:
mw = MainWindow(base).grid()
with:
mw = MainWindow(base)
mv.grid()
Why? grid() does not return anything; implicitly return None.

Python TkInter bind breaking

I have a simple GUI which uses key binds - something like this.
import Tkinter, tkFileDialog
class Foo(object):
def __init__(self, master):
master.bind('3', self.do_bar)
master.bind('9', self.load_new_config)
self.load_config()
if not self.conf:
self.load_new_config()
else:
self.load_data()
def load_config(self):
try:
self.conf = #get stuff from known file
except FailedToGetStuff:
self.conf = None
def load_new_config(self):
path = askopenfilename(initialdir='~')
self.conf = #get stuff from file in path
self.load_data()
def load_data(self):
#get data from self.conf, process and display
def do_bar(self):
#do stuff with displayed data
if __name__ == "__main__"
root = Tk()
Foo(root)
root.mainloop()
Now, this works just fine when load_config() finds what it was looking for. I can use the binds and even after using '9' and loading new config, everything works.
Problem is, if load_config() fails, self.conf gets set to None and load_new_conf gets called from __init__, the binds are no longer operational.
I figured out that the problem is caused by tkFileDialog.askopenfilename() being called from within __init__. What I don't understand is why this happens and how to get around it.
This code works for me:
import Tkinter, tkFileDialog
class Foo(object):
def __init__(self, master):
master.bind('<KeyPress-3>', self.do_bar)
master.bind('<KeyPress-9>', self.load_new_config)
self.load_config()
if not self.conf:
master.after(1, self.load_new_config)
else:
self.load_data()
def load_config(self):
try:
self.conf = None#get stuff from known file
except FailedToGetStuff:
self.conf = None
def load_new_config(self, e = 0):
path = tkFileDialog.askopenfilename(initialdir='~')
self.conf = None#get stuff from file in path
self.load_data()
def load_data(self, e = 0):
pass
#get data from self.conf, process and display
def do_bar(self, e = 0):
print 1
#do stuff with displayed data
if __name__ == "__main__":
root = Tkinter.Tk()
Foo(root)
root.mainloop()

Categories

Resources