Printed text should appear in separate window - How? - python

this question will be relatively simple, just do not get anywhere. It's more of a basic question.
When I write
print ("hello world")
it appears in the script program in the message box bellow. But now I want it to open in a separate fesnter, created with tkinter. I wrote it down like this (see picture or code), but I know that print itself must not be in brackets, how can I solve this problem?
from tkinter import *
a = ("Hello World")
root = Tk()
T = Text(root, height=50, width=150)
T.pack() T.insert(END, print a)
mainloop()
The "hello world" must appear in a separately-opened window.
I am happy about any answer.
Thanks in advance
PyBeginner

I didn't understand your question but can you try this one if it helps .
from tkinter import *
root = Tk()
a = Label(root, text ="Hello World")
a.pack()
root.mainloop()

Resource: https://www.geeksforgeeks.org/python-tkinter-messagebox-widget/ Also, if you want to you can delete showinfo code and just run the w.
from tkinter import *
from tkinter import messagebox
root = Tk()
root.geometry("400x300")
w = Label(root, text='Hello World!', font="50")
w.pack()
messagebox.showinfo("Hello World", "This is a Hello World message!!!")
root.mainloop()

Related

I have a weird problem with tkinter when using python

I am trying to learn how to use Tkinter, but whenever I want to execute my code I always get this problem: (NameError: name 'label' is not defined) or (NameError: name 'button' is not defined).
As a beginner, it seems to me that the problem is with my code editor. (BTW I am using VScode)
this is my code:
from tkinter import *
root = Tk()
mylabel = label(root, text='Hello World')
mylabel.pack()
root.mainloop()
And as I said, this also happens with this one:
from tkinter import *
root = Tk()
mybutton = button(root, text='Hello World')
mybutton.pack()
root.mainloop()
you have caps error its Button and Label not button and label

Why is exit command is not quitting sometimes in python tkinter exe?

In some os's, When you make a tkinter window and put it to mainloop, and make a button for exit and convert it to exe (with pyinstaller) like this:
from tkinter import *
def exit_():
window.destroy()
exit()
window = Tk()
text = Label(window, text = "Hello World")
text.pack()
window.protocol('WM_DELETE_WINDOW', exit_)
window.mainloop()
If you use the built-in exit() command, then sometimes the window will not get closed.
Is there an Answer for that?
Yes there is an answer for it.
Don't use exit() in tkinter exe.
For that use sys.exit() the built-in module in python.
The full code:
from tkinter import *
from sys import exit as sexit
def exit_():
window.destroy()
sexit()
window = Tk()
text = Label(window, text = "Hello World")
text.pack()
window.protocol('WM_DELETE_WINDOW', exit_)
window.mainloop()

using tkinter module with python 3.6

I'm trying to write simple things in a Tkinter module using python 3.6 in Anaconda. This is my code
from tkinter import *
root = Tk()
thelabel = label(root, text="this is our tk window")
thelabel.pack()
root.mainloop()
but I get the error:
TclError: can't invoke "label" command: application has been destroyed
ERROR: execution aborted
and I keep getting a blank Tkinter window whenever I run my code and I don't understand what is the problem, thanks:)
You have a small error...
Here is the correct way:
from tkinter import *
root = Tk()
thelabel = Label(root, text="this is our tk window")
thelabel.pack()
root.mainloop()
You should try reading a bit on tkinter.
Here are some references specifically on label.
Hope you find this helpful!
change your code to this
from tkinter import *
root = Tk()
thelabel = Label(root, text="this is our tk window")
thelabel.pack()
root.mainloop()
Labels start with a capital L not small l it will fix your error

How to create a tkinter error message box [duplicate]

This question already has answers here:
How to create a message box with tkinter?
(5 answers)
Closed 5 years ago.
I am wanting to create a simple message box in Tkinter that appears and displays the exact error message. Could anyone direct me to how this might be
achieved in tkinter, I have not been able to find much on this topic.
E.g:
traceback.format_exc().replace(':', '-')
ctypes.windll.user32.MessageBoxW(0, "Error", "Have you checked your fridge?"d, 1)
^
#'SyntaxError: invalid syntax'
I am wanting to add this with pyinstaller. I suppose pyinstaller creates a text file and you can see in cmd before it closes, but it would be nice if a message box appear with exact traceerror.
from tkinter import messagebox
messagebox.showerror("Title", "Message")
check here for more info
This login system which will pop up messagebox when you provide wrong data for entry messagebox should be entered into the entry if not the messagebox will pop up prompting you an error as occured
from tkinter import *
from tkinter import messagebox
def top():
if entry1.get() == "messagebox":
log.destroy()
root.deiconify()
else:
messagebox.showerror("error", "try again")
messagebox.showinfo("my message","this is an example of showinfo\nmessagebox")
messagebox.showwarning("warning", "show warning example in tkinter" )
root = Tk()
root.geometry("400x400")
log = Toplevel(root)
log.geometry("200x200")
label1 = Label(log, text="password")
entry1 = Entry(log)
button1 = Button(log, text="login", command=top)
label1.pack()
entry1.pack()
button1.pack(side="bottom")
lab = Label(root, text="welcome bro").pack()
root.withdraw()
root.mainloop()

Python Tkinter error - can't invoke "bind" command

I'm trying to bind a button to a simple function.
This is my code:
from Tkinter import *
root=Tk()
def printName(event):
print 'hi my name is Beni'
button_1.bind("<Button-1>",printName)
button_1.pack()
root.mainloop()
The error I get is:
TclError: can't invoke "bind" command: application has been destroyed
Any ideas?
You need to define button_1.
For example:
button_1 = Button(root, text="ButtonName")
So your entire code snippet would be:
from Tkinter import *
root=Tk()
def printName(event):
print('hi my name is Beni')
button_1 = Button(root, text="ButtonName")
button_1.bind("<Button-1>",printName)
button_1.pack()
root.mainloop()

Categories