In python i have been making a text editor like Microsoft word but i don't know how to make a text entry box for the user to put input. Here is my code! (ps thank you!)
from tkinter import *
import sys
def doNothing():
print("Test")
root = Tk()
root.title("TextEditor")
root.geometry("300x200")
menu = Menu(root)
root.config(menu=menu)
subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="New Project...", command =doNothing)
subMenu.add_command(label="Save", command=doNothing)
subMenu.add_separator()
editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Undo",command=doNothing)
root.mainloop()
You can do that like this:
TextArea = Text()
TextArea.pack(expand=YES, fill=BOTH)
If you want a scrollbar with it:
TextArea = Text()
ScrollBar = Scrollbar(root)
ScrollBar.config(command=TextArea.yview)
TextArea.config(yscrollcommand=ScrollBar.set)
ScrollBar.pack(side=RIGHT, fill=Y)
TextArea.pack(expand=YES, fill=BOTH)
Hope this helped, good luck!
It is an old question but currently following is a very good method for scrollable multiline text entry:
ScrolledText(mainwin, width=50, height=5).pack()
Full program:
from tkinter import *
from tkinter.scrolledtext import ScrolledText
mainwin = Tk()
ScrolledText(mainwin, width=50, height=5).pack()
mainwin.mainloop()
Following demo application shows its usage further and comparison with entry box (for python3):
from tkinter import *
from tkinter.scrolledtext import ScrolledText
mainwin = Tk()
Label(mainwin, text="An Entry Box:").grid(row=0, column=0)
ent = Entry(mainwin, width=70); ent.grid(row=0, column=1)
Button(mainwin, text="Print Entry", command=(lambda: print(ent.get()))).grid(row=0, column=2, sticky="EW")
Label(mainwin, text="ScrolledText Box:").grid(row=1, column=0)
st = ScrolledText(mainwin, height=5); st.grid(row=1, column=1)
Button(mainwin, text="Print Text", command=(lambda: print(st.get(1.0, END)))).grid(row=1, column=2, sticky="EW")
Button(mainwin, text="Exit", command=sys.exit).grid(row=2, column=0, columnspan=3, sticky="EW")
mainwin.mainloop()
Related
I'm trying to make the entry widget show once the checkbutton is checked and hide when it's not.
from tkinter import *
master = Tk()
master.geometry('500x400')
other = IntVar()
Checkbutton(master, text="Other", variable=other, command=toggle()).grid(row=10, sticky=W)
def toggle():
if other.get()==1:
Entry(master,width=50).grid(row=11, sticky=W)
else:
Entry(master,width=50).grid_remove()
Try this:
from tkinter import *
def toggle():
if other.get():
ent.grid(row=11, sticky=W)
else:
ent.grid_forget()
master = Tk()
master.geometry('500x400')
other = BooleanVar()
Checkbutton(master, text="Other", variable=other, command=toggle).grid(row=10, sticky=W)
ent=Entry(master,width=50)
master.mainloop()
Python 3.8, Win 10 is the os, Toplevel widget does not appear to be working with new window not appearing. Any guidance would be appreciated, thanks!
from tkinter import *
root = Tk()
def popup():
top = Toplevel(root)
my_label_top = Label(top, text="This is a Tkinter Popup")
top.mainloop()
my_button = Button(root, text="Popup, click here", command="popup")
my_button.grid(row=0, column=0)
root.mainloop()
Problem:
The only issue here is that the callback command shouldn't be a string.
Solution:
Remove the quotes around popup and the Toplevel window should appear.
Fixed Code:
from tkinter import *
root = Tk()
def popup():
top = Toplevel(root)
my_label_top = Label(top, text="This is a Tkinter Popup")
my_label_top.pack()
my_button = Button(root, text="Popup, click here", command=popup)
my_button.grid(row=0, column=0)
root.mainloop()
Tips:
Using top.mainloop() is not necessary.
You also forgot to pack() the Label(my_label_top)
There is something in my code that isn't letting me use the text from my Entry widget. I want to use the text a user will enter in my Entry widget, which i've called "textentry", in another part of my code but it doesn't seem to be storing it in a way i can use. In this example i'm just trying to print what is entered into the terminal.
I can get it to print if i uncomment the "print(textentry.get())" in my function.
As it is now i get ".!entry" printed in the terminal. i'm not sure I follow that output either.
I feel like it's probably something simple but i've been struggling a while and many different approaches but still not success.
import tkinter as tk
from tkinter import *
def click():
textentry.get()
# print(textentry.get())
Text_input_window.destroy()
Text_input_window= Tk()
Label (Text_input_window,text="Enter search word:", bg="black", fg="white").grid(row=1, column=0, sticky=W)
textentry = tk.Entry(Text_input_window, width=20, bg="white")
textentry.grid(row=2, column=0, sticky=W)
Button(Text_input_window, text="SUBMIT", width=6, command=click).grid(row=3, column=0, sticky=W)
Text_input_window.mainloop()
print(textentry)
Try it:
import tkinter as tk
from tkinter import *
Text_input_window = Tk()
textentry = StringVar()
def click():
global textentry
textentry = textentry_ent.get()
# print(textentry.get())
Text_input_window.destroy()
Label(Text_input_window, text="Enter search word:",
bg="black", fg="white").grid(row=1, column=0, sticky=W)
textentry_ent = tk.Entry(Text_input_window, textvariable=textentry,width=20, bg="white")
textentry_ent.grid(row=2, column=0, sticky=W)
Button(Text_input_window, text="SUBMIT", width=6,
command=click).grid(row=3, column=0, sticky=W)
Text_input_window.mainloop()
print(textentry)
I am making a program for finding booking contacts with ease. When I type in all of this information, the buttons stay to the top left of the window.
How can I get them in the middle of the screen? Better, how would I place them where-ever I want in general? (Don't worry about the webbrowser import for that is for later in the program.)
import webbrowser
from tkinter import *
from tkinter import ttk
root = Tk()
root.title('GUI Booking')
root.geometry('600x400')
root.resizable(width=False, height=False)
style = ttk.Style()
style.configure("TButton",
font="TkDefaultFont",
height=20,
width=20,
padding=10)
main_frame = Frame()
main_frame.grid(row=0, columnspan=4)
# Starting Window
button_location = ttk.Button(main_frame, text='Location').grid(row=1, column=3)
button_name = ttk.Button(main_frame, text='Name').grid(row=2, column=3)
button_email = ttk.Button(main_frame, text='Email').grid(row=3, column=3)
root.mainloop()
use the .place option instead of .grid:
...
Button = Button(something, text='Something', command=Something)
Button.place(x=value(e.g:2), y=value(e.g:2))
...
You could use row/column configure to center up the buttons: http://www.effbot.org/tkinterbook/grid.htm
I've added this under your root configuration. Ultimately, the position of widgets will depend on the widgets that you use and adjusting their position with the grid manager.
import webbrowser
from tkinter import *
from tkinter import ttk
root = Tk()
root.title('GUI Booking')
root.geometry('600x400')
root.resizable(width=False, height=False)
root.grid_rowconfigure(0, weight=1) # Added to center buttons
root.grid_columnconfigure(0, weight=1) # Added to center buttons
style = ttk.Style()
style.configure("TButton",
font="TkDefaultFont",
height=20,
width=20,
padding=10)
main_frame = Frame()
# Starting Window
button_location = ttk.Button(main_frame, text='Location').grid(row=1, column=1)
button_name = ttk.Button(main_frame, text='Name').grid(row=2, column=1)
button_email = ttk.Button(main_frame, text='Email').grid(row=3, column=1)
main_frame.grid(row=0, column=0)
root.mainloop()
if you want them to me in the middle of the screen, use .pack instead of .grid.
import webbrowser
from tkinter import *
from tkinter import ttk
root = Tk()
root.title('GUI Booking')
root.geometry('600x400')
root.resizable(width=False, height=False)
style = ttk.Style()
style.configure("TButton",
font="TkDefaultFont",
height=20,
width=20,
padding=10)
main_frame=Frame()
# Starting Window
button_location = ttk.Button(main_frame, text='Location').grid()
button_name = ttk.Button(main_frame, text='Name').grid()
button_email = ttk.Button(main_frame, text='Email').grid()
main_frame.pack()
root.mainloop()
warning: do not use .pack and .grid in the same window.
How can I create a Modal Dialogue Box by messagebox.showerror?
messagebox.showerror("Error", "No downloader.exe found")
When I create a messagebox, I found I can move the root windows.
and i need to create a Modal Dialogue Box like filedialog.askopenfilename.
filedialog.askopenfilename(initialdir = self.get_path()+ '/bin', filetypes=[("BIN Files", ".bin")])
here's the codes:
import tkinter
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os
class Application(Frame):
def createWidgets(self, main_frame):
#self.llabel = Label(main_frame, text="Ready", width=20, bg="turquoise", font = ftLabel)
#self.llabel.grid(row=0, column=0, sticky=W+E) #columnspan=2
self.frame1 = Frame(main_frame)
self.frame1.grid(row=0, column=0, columnspan=2, sticky=W+E+N+S)
self.addr = StringVar()
self.addrtext = Entry(self.frame1, width=20, textvariable = self.addr)
self.addrtext.grid(row=0, column=0, sticky=W+E+N+S)
self.addr.set("0x0")
self.bfile = Button(self.frame1, text='BIN File', width=20)
self.bfile.grid(row=0, column=1, sticky=W+E+N+S)
messagebox.showerror("Error", "No downloader.exe found")
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack(fill=BOTH, expand=1)
main_frame = Frame(master)
main_frame.pack(fill="y", expand=1)
self.createWidgets(main_frame)
self.dl_thread = 0
if __name__=="__main__":
root = Tk()
#lock the root size
root.resizable(False,False)
app = Application(master=root)
app.mainloop()
I tried your code and messagebox.showerror is modal to me.
Maybe there is something else in your code (threads?) or maybe it's
dependent on your environment.
For reference, my entire code:
from tkinter import *
from tkinter import messagebox
root = Tk()
def do(): messagebox.showerror("Error", "No downloader.exe found")
b = Button(root, text='Dialog', command=do)
b.pack()
root.mainloop()
If that doesn't work you might want to take a look at: Tkinter messagebox not behaving like a modal dialog