Page 1 of 5

Pymaxwell and Tk (tkinter)

Posted: Sun Aug 27, 2017 10:58 pm
by seghier
Hello
i have question about Pymaxwell and Tk ;
in maxwell folder > pYthon > tcl included
can we create window with buttons using Pymaxwell ?
i tried but Pymaxwell don't recognize tkinter

Re: Pymaxwell and Tk (tkinter)

Posted: Mon Aug 28, 2017 11:08 am
by Brany
I've just managed to use tkinter from within pymaxwell.exe, you can try something like this:
Code: Select all
from Tkinter import *
import sys
if not hasattr(sys, 'argv'):
    sys.argv = [sys.executable]
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()

Re: Pymaxwell and Tk (tkinter)

Posted: Mon Aug 28, 2017 12:42 pm
by seghier
thanks alot Brany
it work fine and also work with this :
Code: Select all
from Tkinter import *
sys.argv = [sys.executable]
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
is it important to add this two lines? :
import sys
if not hasattr(sys, 'argv'):
------------
this code to create button is it possible to use a button to load mxs file and use another button to make a change in the scene ?
Code: Select all
from Tkinter import *

sys.argv = [sys.executable]

root = Tk()
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)

button1 = Button(topFrame, text='Button 1', fg='red')
button2 = Button(bottomFrame, text='Button 2', fg='blue')

button1.pack(side=LEFT)
button2.pack(side=BOTTOM)

root.mainloop()

Re: Pymaxwell and Tk (tkinter)

Posted: Mon Aug 28, 2017 1:12 pm
by seghier
there are some examples about ttk in maxwell>python>tcl>tk8.5>ttk folder
this example didn't work in pymaxwell even i add the new line : sys.argv = [sys.executable]
Code: Select all
from tkinter import *
from tkinter import ttk

def calculate(*args):
    try:
        value = float(feet.get())
        meters.set((0.3048 * value * 10000.0 + 0.5)/10000.0)
    except ValueError:
        pass
    
root = Tk()
root.title("Feet to Meters")

mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

feet = StringVar()
meters = StringVar()

feet_entry = ttk.Entry(mainframe, width=7, textvariable=feet)
feet_entry.grid(column=2, row=1, sticky=(W, E))

ttk.Label(mainframe, textvariable=meters).grid(column=2, row=2, sticky=(W, E))
ttk.Button(mainframe, text="Calculate", command=calculate).grid(column=3, row=3, sticky=W)

ttk.Label(mainframe, text="feet").grid(column=3, row=1, sticky=W)
ttk.Label(mainframe, text="is equivalent to").grid(column=1, row=2, sticky=E)
ttk.Label(mainframe, text="meters").grid(column=3, row=2, sticky=W)

for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)

feet_entry.focus()
root.bind('<Return>', calculate)

root.mainloop()

Re: Pymaxwell and Tk (tkinter)

Posted: Mon Aug 28, 2017 3:40 pm
by Brany
is it important to add this two lines? :
import sys
if not hasattr(sys, 'argv'):
import sys -> mandatory if you run your script from ouside pymaxwell.exe (let's say the usual python interpreter installed in the system)
if not hasattr(sys, 'argv'): -> 'argv' does not exists running the script from pymaxwell.exe, but if you run it from a regular python interpreter it will exist, and it can have parameters that you don't want to lose, so you only have to set this in the case it doesnt exist.

In a nutshell, I made this sample script in a way that it can be run in pymaxwell.exe and in any python interpreter installed in the system.
this code to create button is it possible to use a button to load mxs file and use another button to make a change in the scene ?
I have never used Tk before, but you can do that for sure. I've used Qt for python to do such as things.

Re: Pymaxwell and Tk (tkinter)

Posted: Mon Aug 28, 2017 6:14 pm
by seghier
thanks for explanation
this also work in pymaxwell and other python editor ( i tried with cmd and pyCharm )
Code: Select all
from Tkinter import *
sys.argv = [sys.executable]
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
i will try Qt
what i want create from exist script in maxwell folder is to use button to load scene for example and apply a command and run without need to write in pymaxwell

Re: Pymaxwell and Tk (tkinter)

Posted: Tue Aug 29, 2017 10:20 am
by Brany
I'm not sure if you are talking about pyqt_edit_physical_sky.py in %MAXWELL4_ROOT%/scripts/python/, but this is a good starting point if you finally use Qt.

Take into account that pymaxwell doesn't include the PyQt module, so you have to install it in the python interpreter.

Re: Pymaxwell and Tk (tkinter)

Posted: Tue Aug 29, 2017 12:23 pm
by seghier
Thanks
Yes i install it with qt designer
I test an example with pycharm and worked fine but pymaxwell can't run the command.

I aldo tried the script pyqt_edit_physical_sky
But i got an error : no module named sip
Than i got other error
: ...line15...object has no attribute 'QWidget'

Re: Pymaxwell and Tk (tkinter)

Posted: Tue Aug 29, 2017 1:47 pm
by seghier
seghier wrote:
Tue Aug 29, 2017 12:23 pm
pyqt_edit_physical_sky
using pycharm : ImportError: No module named pymaxwell

Re: Pymaxwell and Tk (tkinter)

Posted: Tue Aug 29, 2017 1:53 pm
by seghier
i copy pymaxwell to C:\Python27\lib\site-packages\ and this is the error

Traceback (most recent call last):
File "C:/Users/archi/Desktop/sun2.py", line 7, in <module>
from pymaxwell import *
File "C:\Python27\lib\site-packages\pymaxwell\__init__.py", line 13, in <module>
from new import instancemethod as new_instancemethod
ImportError: cannot import name instancemethod

i also installed pyqt4 with python 3.5 ; but there is difference between pyqt4 and pyqt5

Re: Pymaxwell and Tk (tkinter)

Posted: Tue Aug 29, 2017 4:11 pm
by Brany
you have to use
Code: Select all
from pymaxwell4 import *
You must use "pymaxwell" module instead of "pymaxwell4" from within pymaxwell.exe due a bug, sorry about the inconvenience! It will be fixed for the next release.

Re: Pymaxwell and Tk (tkinter)

Posted: Tue Aug 29, 2017 4:29 pm
by seghier
yes i used : from pymaxwell import*
it work fine using pycharm and i read in the script

# Script must run in a python console, not from within pymaxwell
why pymaxwell can't run the script ?
-------
and i hope see tutorial about using qt designer + pymaxwell and also an update of this script with pyqt5

Re: Pymaxwell and Tk (tkinter)

Posted: Tue Aug 29, 2017 5:49 pm
by seghier
pyqt + pyinstaller create nice and executable script
i will try with other script

Re: Pymaxwell and Tk (tkinter)

Posted: Tue Aug 29, 2017 6:17 pm
by seghier
i create executable file but can't do anything
global name cmaxwell is not defined
Image

Re: Pymaxwell and Tk (tkinter)

Posted: Tue Aug 29, 2017 6:31 pm
by Brany
How did you imported pymaxwell4?

There are two ways:
Code: Select all
from pymaxwell4 import *
mxs = Cmaxwell(mwcallback)
mxs.readMXS('C:/scene.mxs')
Code: Select all
import pymaxwell4 as mw
mxs = mw.Cmaxwell(mw.mwcallback)
mxs.readMXS('C:/scene.mxs')
Hope it helps