My problem is that my python code is not working when I run it as a .py file. Here is the code:
import tkinter
tk=tkinter.Tk()
canvas=tkinter.Canvas(tk, width=500, height=500)
canvas.pack()
There is more code to it than that, but that is the relevant stuff. It works fine when I use the python shell or type it directly into the python console, but when I run it as a .py file, it seems to skip this code and go on to the rest, without displaying a canvas. I am using windows, but I am not sure what version of python I'm using.
I was also using
from * import tkinter
before, with relevant changes to the code and i changed it to try and help fix it. It didn't work :(
You are missing the eventloop at the end:
import tkinter
tk=tkinter.Tk()
canvas=tkinter.Canvas(tk, width=500, height=500)
canvas.pack()
# Enter into eventloop <- this will keep
# running your application, until you exit
tk.mainloop()
Only a personal recommendation: don't use tk as a variable name, use app or root or even win/window
Related
I started studying Python last Monday. I tried to create a window using tkinter. Pycharm says there are no mistakes but after running the code the window doesn't appear D:
That's my code
from tkinter import *
window = Tk()
window.title("thecharmii")
window.geometry("700x500")
window.config(background = "black")
window.mainloop()
I downloaded the latest Python version, I guess it's 3.10. When I click on Run the window doesn't appear but it looks like the process is still running. Thanks for the help
try this
from tkinter import *
def main():
window = Tk()
window.title("thecharmii")
window.geometry("700x500")
window.config(background = "black")
window.mainloop()
main()
It works completely fine with me.
Try running it in your cmd/powershell/terminal.
If it is running, then it might be that the configurations for the file you are working with in pycharm are messed up.
I am running my performance test on python and I want to call some Tcl commands using Tkinter inside my python script.
Can somebody please help me to write the code,
import Tkinter
root = Tkinter.Tk()
root.tk.eval('puts {printed by tcl}')
I tried simply above example, here when I do root=tkinter.tk() it opens up a window, I just want to execute my command and get the result
The code you have tried will not show any window until you put the root.mainloop(),but you can try something like this,
import tkinter
root = tkinter.Tk()
root.withdraw()
root.tk.eval('puts {printed by tcl}')
root.destroy()
root.mainloop()
here withdraw() will remove the window from the screen without destroying it and then you can perform your tasks and then destroy it at the end of code.
I'm building an tkinter app on Python 3.7 and creating an .exe with Pyinstaller 3.5 in Windoows 10. When running the code from the IDE, all intended keyboard commands work as expected. However, in the executable, key combinations do not work while single key presses are fine.
Here is some test code that demonstrates the problem:
import tkinter as tk
root = tk.Tk()
txt = tk.StringVar()
lbl = tk.Label(root, textvariable=txt)
def key_handle(event):
global txt
txt.set(event.keysym)
def kc_handle(event):
tk.messagebox.showinfo('Key Combo', 'Key Combo pressed')
root.bind('<Key>', key_handle)
root.bind('<Alt-b>', kc_handle)
lbl.pack()
root.mainloop()
Pyinstaller is then invoked as pyinstaller -w -F key_test.py.
One thing I do know is that the order of the binds doesn't seem to make a difference. How do I get key combinations working in the executable?
IDLE is built using tkinter and it could import all needed modules for own use and your code could work correctly but when you run it without IDLE then you have to import all modules which you use in code. In your example it will be
import tk.messagebox
BTW: Often similar problem is with mainloop(). IDLE aready runs mainloop() so code may work without own mainloop(). But normally (without IDLE) it needs to use mainloop(). It is good to check code in terminal/console/cmd.exe to see if it gives any errors.
Very new to python, just trying to create a Canvas in python using tkinter:
from tkinter import *
top = Tk()
w = Canvas (top , height = 300, width = 300)
Yields error:
_tkinter.TclError: no display name and no $DISPLAY environment variable
Most of what I saw regarding this issue had to do with running code on a remote machine and not having a place to display the output. I was running this on the online compiler:
https://repl.it/repls/MintyHumongousParentheses
Any insight into this error is appreciated, thank you!
The tkinter package is a thin object-oriented layer on top of Tcl/Tk. The problem is Tcl can't find the virtual display.
As stovfl pointed out your problem is the online python editor, I don't know witch one you are using, but repl.it is compatible with remi.gui, see example: https://repl.it/#amasad/tictactoe.
I tested your code locally and it didn't work, so I made a few changes:
from tkinter import *
top = Tk()
w = Canvas(top, width=300, height=300)
w.pack()
top.mainloop()
How do I define root for tkinter in Python 3? I'm using an online Python editor
Tkinter
Python Tkinter not working in a .py file
I need to embed an interative python interpreter into my tkinter program. Could anyone help me out as to how to integrate it?
I have already looked at the main() function, but it's way to complex for my needs, but I can't seem to reduce it without breaking it.
Some details of what you must do may depend on what you want to do with IDLE's Shell once you have it running. I would like to know more about that. But let us start simple and make the minimum changes to pyshell.main needed to make it run with other code.
Note that in 3.6, which I use below, PyShell.py is renamed pyshell.py. Also note that everything here amounts to using IDLE's private internals and is 'use at your own risk'.
I presume you want to run Shell in the same process (and thread) as your tkinter code. Change the signature to
def main(tkroot=None):
Change root creation (find # setup root) to
if not tkroot:
root = Tk(className="Idle")
root.withdraw()
else:
root = tkroot
In current 3.6, there are a couple more lines to be indented under if not tkroot:
if use_subprocess and not testing:
NoDefaultRoot()
Guard mainloop and destroy (at the end) with
if not tkroot:
while flist.inversedict: # keep IDLE running while files are open.
root.mainloop()
root.destroy()
# else leave mainloop and destroy to caller of main
The above adds 'dependency injection' of a root window to the function. I might add it in 3.6 to make testing (an example of 'other code') easier.
The follow tkinter program now runs, displaying the both the root window and an IDLE shell.
from tkinter import *
from idlelib import pyshell
root = Tk()
Label(root, text='Root id is '+str(id(root))).pack()
root.update()
def later():
pyshell.main(tkroot=root)
Label(root, text='Use_subprocess = '+str(pyshell.use_subprocess)).pack()
root.after(0, later)
root.mainloop()
You should be able to call pyshell.main whenever you want.