I'm new to Tkinter, Python and Linux. I'm running Ubuntu 19.04 and trying to run my Python-Tkinter code in Atom, but when I run it, only terminal shows up with an information about successful execution and execution time. No canvas appears.
import tkinter
canvas = tkinter.Canvas(width=200, height=200, bg="white")
canvas.pack()
canvas.create_line(0,0,100,100)
I expect new window with canvas and line to appear, but that doesn't happen, I only get terminal saying:
Process returned 0 (0x0) execution time : 0.307 s
Press [ENTER] to continue...
Thank you for your help.
Like PRMoureu and furas said, I only added canvas.mainloop() at the end and that solved my problem! Thank them!
import tkinter
canvas = tkinter.Canvas(width=200, height=200, bg="white")
canvas.pack()
canvas.create_line(0,0,100,100)
canvas.mainloop()
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 keep on encountering problems with Tkinter in spyder and I am unsure why. Repeatedly I'd spent a lot of time trying to fix code in spyder which is fully operational in IDLE. Is there a simple reason for this? Is my code wrong and IDLE is ignoring the incorrect bits while spyder is having an issue. When running the code there is never an error in spyder it just doesn't operate as expected. I will show some code that displays differently in IDLE than it does spyder.
from tkinter import font
import tkinter as tk
root = tk.Tk()
root.title("N Body Simulation")
root.geometry('400x300')
frame = tk.Frame(root)
frame.pack()
# titlefont = font.Font(family="Lucida Grande",size=45)
titlefont = font.Font(family='Helvetica', name='titlefont', size=50, weight='bold')
text_intro = "N-Body Simulation"
title = tk.Label(root, text=text_intro, font=titlefont)
title.place(relx=0.5, rely=0.1, anchor="center")
root.mainloop()
The image on the top is from spyder and the one on the bottom is IDLE. Why does it keep appearing different, and what am I doing wrong. To me, I have done the necessary steps to change the font size and make it bold. Also is there a way to run it like IDLE but though spyder, sorry if that last part is a silly question. Thanks in advance.
I would like the following program to quit on <Any-KeyPress> event.
from tkinter import *
root = Tk()
root.overrideredirect(True)
root.bind('<Any-KeyPress>', lambda e: root.destroy())
root.mainloop()
This works fine on Windows OS. However this does not work on Ubuntu unless I remove the line root.overrideredirect(True) from the above code.
Is this the intended behavior ?
Or is there a way whereby I can make my program to work while still using root.overrideredirect(True) ?
Edit
I just saw a similar question here at SO, where Bryan Oakley suggests using root.focus_force() but it does not help.
Edit 2
I used root.attributes('-fullscreen', True) instead of root.overrideredirect(True) as suggested here and that seems to work now.
Try this:
from tkinter import *
root = Tk()
root.bind('<Any-KeyPress>', quit())
root.mainloop()
Assuming that you want the program to quit, keep the code. If you just want to clear the screen, just use root.destroy() rather that quit(). Using root.overrideredirect(True) will NOT work on Ubuntu.
I am able to get a frame to open from a button. I can close the frame and reopen it from the same button but it throws an error everytime I push the button.
What is throwing the error in my code is root.Show() , it gives me a AttributeError Show error
My question is, although it is working beautifully, could it develop a serious problem for my application?
EDIT: This is the code in my python file
from Tkinter import *
root = Tk()
root.title("Help")
scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
help_message = 'This is the help menu. Please scroll through the menu to find the answer to your question'
txt = Text(root, wrap=WORD) # wrap=CHAR, wrap=NONE
txt.pack(expand=1, fill=BOTH)
txt.insert(END, help_message)
txt.config(yscrollcommand=scrollbar.set, state=DISABLED)
scrollbar.config(command=txt.yview)
root.Show()
It is taking the error from the last line of this program. And this is the error in the command prompt:
You probably just want to use root.mainloop(), because I don't see anywhere the method Show or show (Python is case sensitive, and methods/functions are usually lower_case_with_underscores).
The mainloop function essentially waits for the program to end, but if you have things like buttons, you can have Tkinter call a certain function to respond (event driven).
I removed root.Show(), added a button that has to be clicked in order for the window to stay hidden but running. I then added in the main file HelpBox.root.deiconify() which makes the screen pop up. If the user does click the "X" button, then the help menu cannot be reopened until they restart the application.
Thanks for everyone's help and ideas
#!/usr/bin/env python
# Display window with toDisplayText and timeOut of the window.
from Tkinter import *
def showNotification(notificationTimeout, textToDisplay):
## Create main window
root = Tk()
Button(root, text=textToDisplay, activebackground="white", bg="white", command=lambda: root.destroy()).pack(side=LEFT)
root.update_idletasks()
# Remove window decorations
root.overrideredirect(1)
timeOut = int(notificationTimeout*1000) # Convert to ms from s
## Run appliction
root.after(timeOut,root.destroy)
root.mainloop()
The above code creates a notification, with a timout. However on windows - the notification does not automatically pop up above all other present windows automatically. One has to click on the kill button (the text), and focus it the first time, after which the root window will be displayed above all other windows.
Is there a way to make the notification automatically appear above all other windows - on windows?
It seems to work on linux just fine (ubuntu 9.10).
According to this message you should be able to add the following after root.overridedirect(1). A quick test here suggests it should work for you.
root.wm_attributes("-topmost", 1)