I followed this post to write a simple gui program in my Spyder IDE on anaconda environment.I am using python 3.7. I used below code :
from tkinter import Tk
from tkinter.filedialog import askopenfilename
Tk().withdraw()
filename = askopenfilename()
print(filename)
and
import easygui
print easygui.fileopenbox()
Both of the code seems to run indefinitely and doesn't open any window. Am i missing something? Is it even possible to make a gui program on anaconda environment?
Related
I am completely new to Python. The basics are pretty clear at the moment. I am trying to build a simple GUI for the first time and ran into tkinter. I copied a bit of code and ran it. It works.
from tkinter import *
from tkinter import ttk
root = TK()
root.title("test")
Then I wanted to alter and add code, created a new file and started typing:
from tkinter import *
from tkinter ttk
root = TK()
root.title("smartDisplay")
And guess what... It doesn't work. It says "name 'TK' is not defined". It is the same Mac the same IDE, the same folder of the two files. So what is going on here? What am I missing?
I am using python 3.8.2 on a Mac with 10.15.7
Python is case sensitive and needs root = Tk(). Also I didn't see a root.mainloop() at the end of your code. That is also needed to run the Tkinter window.
Batch code:
TITLE %~nx0
python "c:\users\aaa\desktop\coding\python\first project with tkinter\buttomtest.py"
pause
and python code is:
from tkinter import *
window = Label(text="gg")
print("tt")
and this is what I get:
It shows the cmd prompt window but it doesn't show the window of tkinter
Tkinter is a python module meant ONLY for python.
Now I see what you tried to do but learning python may be the way there is an OS module import OS which could allow you to run command prompt commands. os.system("command here") will allow you to run commands from the system DOS.
Hope this helps and enjoy the community!
You may also run a start command
start File.py
Exit
way to fix that: window.mainloop()
and there is no need for import OS
I'm learning to use Tkinter for another program I'm writing. This code works in Spyder and on a friends Jupyter notebook, but it does't work on mine. Is there something in the settings of my Jupyter notebook I need to change to make it work?
This is the error message I get:
TclError: no display name and no $DISPLAY environment variable
from tkinter import *
from tkinter.ttk import *
window = Tk()
window.title("Hello World")
window.geometry('1500x1500')
window.mainloop()
My script runs smoothly. However after compiling with Pyinstaller, launching the .exe and clicking the start button, the GUI opens once again and crashes.
def grabberfunc(*args):
im = ImageGrab.grab()
savedir=str(mappa)
savefile="Screenshot_"+str("{:%Y_%m_%d-%H_%M_%S}".format(datetime.datetime.now()))+".png"
savedirfile=join(savedir,savefile)
im.save(str(savedirfile))
def scanning():
interval=deftimeInput.get()
if running:
grabberfunc()
root.after(int(interval)*1000, scanning)
if __name__=='__main__':
root = tkinter.Tk()
...
There is a basic GUI with 2 buttons: 'Start' sets the 'running' variable 'True', the 'Stop' vice versa.
If the script runs the GUI doesn't open up again and runs as I want it to.
Finally found the solution.
Instead of importing the pyscreenshot module, the ImageGrab module should be imported from PIL
So the correct import is:
from PIL import ImageGrab
After compiling the script by pyinstaller, the exe runs fine.
Hi i was wondering if you could have the comand prompt box pop into the Tkinter window when you start the program? Some thing like:
from Tkinter import *
admin = Tk()
cmd = Cmd(admin)
cmd.pack()
admin.mainloop()
I'm on windows
http://tkinter.unpythonic.net/wiki/CmdTkHere is what you want, this is not just opening a cmd window. its embedding cmd.exe into a Tkinter.Frame. And a note here if you rename the python script to ".pyw" extension, the console will be hidden. Except for in a virtual environment's.
I don't believe there is any built in console widget. It may be possible to whip up a custom one using the Tkinter Text widget. However, that would take a bit of effort/time.
Another possible option is simply have your program launch command prompt.
Two different ways to launch command prompt on a Windows machine.
import subprocess, os
subprocess.Popen('cmd.exe')
os.system("cmd.exe")
EDIT:
Unforunetly I don't believe there is any built in widget like that. However I thought of another possible solution, check out the code for the IDLE GUI, it has a console and the GUI portion is entirley written using Tkinter. So you may be able to utelize that code.