Python tkinter print to GUI [duplicate] - python

This question already has answers here:
stdout to tkinter GUI
(3 answers)
Closed 6 years ago.
How can I print text to my GUI instead of in the console with tkinter?
For example if I print "Test" instead of printing Test in my console it should print Test to the next line of the GUI. I need this to work with a large number of printed lines.

NOTE: I'm not sure what you mean by 'print', if this isn't what you're looking for please add a comment
As for 'printing' on your tkinter window, there are a few different ways.
One good way is the label widget, this is a widget which contains text, you can change the font, size, colour and alignment of the text.
mylabel = Label(master, text = "ExampleText", font = ("Purisa", 12)) # master can be a window or a frame
mylabel.pack() # packs the label on to the master
Another method is creating text on a canvas
mycanvas = Canvas(...)
mycanvas.create_text(x = 100, y = 100, text = "ExampleText")

Related

Printing out information with python? [duplicate]

This question already has answers here:
send the output of print() to tkinter Text widget
(3 answers)
Closed yesterday.
I'm trying to print out information into this window and not in the console. How do I do this?
from tkinter import *
print("Hello")
gui = Tk(className='StreetView Map')
# set window size
gui.geometry("500x200")
gui.mainloop()
I tried using the print function but nothing showed up in the window.
You can create a Label.
Label(gui, text="Hello").pack()
You need to Create a Text widget where you can insert the test on GUI on Tkinter.
Given your code it would have to be done like this.
from tkinter import *
gui = Tk(className='StreetView Map')
# set window size
gui.geometry("500x200")
T = Text(root, height = 5, width = 52)
T.pack()
#insert the string in Text Widget
T.insert(tk.END, "hello")
gui.mainloop()

how to get the list of the available fonts on Tkinter? [duplicate]

This question already has answers here:
List available font families in `tkinter`
(11 answers)
Closed 1 year ago.
how do I get a list of the names of the available font in Tkinter?
The only fonts that I tried and worked were 'Courier' and 'Helvetica', and I tried using other fonts but it returned the same default fonts.
# this works
button = tk.Button(self.subframe, text="quit", command=master.destroy,
highlightbackground='black', font='Helvectica 18')
# this does not work (the text remains the font)
button = tk.Button(self.subframe, text="quit", command=master.destroy,
highlightbackground='black', font='Roboto 18')
(font used in the second example)
so I don't know which fonts are available or not, and it would be helpful if I can get a list of the fonts.
You can do like this
from tkinter import Tk, font
root = Tk()
print(font.families())

How can I detect if a user click a tkinter button? [duplicate]

This question already has answers here:
How to handle a click button event in python module tkinter
(2 answers)
Closed 2 years ago.
Here is my code!
///
from tkinter import *
import os
import pygame
os.system(‘clear’)
#Window Setup
root = Tk()
root.title(‘cottontail’)
root.geometry(‘800x600’)
frame = Frame(root)
title_screen = Label(root, text = “Choose your rabbits name!”)
title_screen.pack()
name = Text(root, width=10 , height=3)
name.pack()
confirm_name = Button(root, text= “Conirm?”, width = 5, height=3)
root.mainloop()
///
My objective is to take the input the user puts in the text box to make a label in a pygame window with that name. I figured that a button would be an easy way to confirm the name and open the pygame screen. If this makes any sense to you it would really be appreciated if you could help me. Hope you have a good night!
Thanks in advance!
I'm not familiar with tkinter as I'm a PyQt dev, but I can help with your question as the logic behind both of them are same.
You can make a function that is called by the button whenever its clicked, and then, inside that function definition, do whatever you want.
Here's the website that explains this in detail -
https://www.delftstack.com/howto/python-tkinter/how-to-get-the-input-from-tkinter-text-box/

How can i enumerate through tkinter label and highlight or underline each word [duplicate]

This question already has an answer here:
Python tkinter single label with bold and normal text
(1 answer)
Closed 4 years ago.
I am looking for a way to underline each word in a label text per condition I've set.
I looked for similar questions posed before and I've only managed to either Underline the entire text at once or one single letter. My app is essentially supposed to be a text typing application. For now i'm tracking each word, I would like to Underlining the upcoming word. here's my code.
from tkinter import *
from tkinter import font
class App:
def __init__(self):
self.root = Tk()
self.count = 0
l = Label(text="Hello, world")
l.pack()
f = font.Font(l,l.cget("font"))
f.configure(underline = True)
l.configure(font=f)
self.root.mainloop()
if __name__ == "__main__":
app=App()
For example purposes one can use the self.root.after(mili, method)
I'm using tkinter python 3.6.6
Thanks in advance.
You can't highlight individual words in a Label widget. If you need that capability, you will need to use a Text widget.

How to make default text in entry that disappears [duplicate]

This question already has answers here:
Entry box text clear when pressed Tkinter
(2 answers)
Closed 6 years ago.
For instance:
from Tkinter import *
root = Tk()
e1 = Entry(root)
e1.insert(END, "ex. new file") #would like to make this text disappear when clicked
e1.grid(row=0, column=0)
root.mainloop()
Where the text "ex. newfile" disappears when clicked upon, leaving a blank entry field.
Create a boolean flag that monitors if the entry has been accessed; set it to False,
Bind "<Button-1>" to a function that clears the entry if it has not been accessed yet, and changes the flag to True.
Added
def delete_text(event):
if default_text:
e1.delete(0, END)
default_text = False
default_text = True
e1.bind("<Button-1>", delete_text)
Thanks to DYZ and effbot

Categories

Resources