I am trying to put a frame inside another frame in tkinter. Can someone explain why it isn't working? I am just starting to learn Tkinter.
from tkinter import *
from PIL import *
if __name__=="__main__":
root = Tk()
root.title("Sales")
root.geometry("1440x855")
root.resizable(0, 0)
Label(root, text = 'Tax Invoice').pack(side = TOP, pady = 6)
frame1 = Frame(root,bg="black",width=1400,height=780).pack()
frame2 = Frame(frame1,bg="green",width=100,height=100).pack()
top.mainloop()
Try this:
import tkinter as tk
if __name__ == "__main__":
root = Tk()
root.title("Sales")
root.geometry("1440x855")
root.resizable(False, False)
label = tk.Label(root, text="Tax Invoice")
label.pack(side="top", pady=6)
frame1 = Frame(root, bg="black", width=1400, height=780)
frame1.pack()
frame2 = Frame(frame1, bg="green", width=100, height=100)
frame2.pack()
root.mainloop()
Basically applying what #Ramesh and I said in the comments.
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()
I made a text widget to write questions. I want to insert images when add image button is pressed. How can I do that? When I choose another image the first one is deleted. Right now I am able to add one image using the code:
import tkinter as tk
root = tk.Tk()
root.geometry('800x520+0+0')
global img
img = tk.PhotoImage(file="quiz.gif")
def add_img():
T.image_create(tk.INSERT, image=img)
tk.Button(root, text="Add Image", font=('Verdana',8),
command=add_img).place(x=690, y=0)
T = tk.Text(root, width=65, height=17, padx=10, pady=10, font=('Verdana',
14), wrap='word')
T.place(x=0, y=0)
root.mainloop()
I want to add different images when chosen using tk-listbox.
Thanks for the help.
I used dictionary to solve the problem.
import tkinter as tk, glob
root = tk.Tk()
root.geometry('800x520+0+0')
Images = {}
for infile in glob.glob('*.gif'):
img = infile[:-4]
if img not in Images:
Images[img] = tk.PhotoImage(file=infile)
def add_img():
global listbox
listbox = tk.Listbox(root, font=('Verdana',9), width=12)
listbox.place(x=60,y=2)
for infile in glob.glob('*.gif'):
listbox.insert(tk.END, infile[:-4])
listbox.bind('<<ListboxSelect>>',CurSelet)
def CurSelet(event):
fn = listbox.get(tk.ANCHOR)
listbox.destroy()
T.image_create(tk.INSERT, image=Images[fn])
tk.Button(root, text="Add Image", font=('Verdana',8),
command=add_img).place(x=690,y=0)
T = tk.Text(root, width=65, height=17, padx=10, pady=10, font=('Verdana',
14), wrap='word')
T.place(x=0, y=50)
root.mainloop()
The code picture needs to fit the screen size perfectly. I saw a bunch of tutorials but nothing seems to work.I tried adding a canvas but it covers half the screen.All my buttons go under the image itself not over it. It's getting on my nerves .
here's my code :
import tkinter as tk
import PIL
from PIL import Image, ImageTk
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
root = Tk()
w = Label(root, text="Send and receive files easily")
w.config(font=('times', 32))
w.pack()
def create_window():
window = tk.Toplevel(root)
window.geometry("400x400")
tower= PhotoImage(file="D:/icons/tower.png")
towlab=Button(root,image=tower, command=create_window)
towlab.pack()
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.master.title("Bifrost v1.0")
self.pack(fill=BOTH, expand=1)
self.img1 = PhotoImage(file="D:/icons/download.png")
self.img2 = PhotoImage(file="D:/icons/upload.png")
sendButton = Button(self, image=self.img2)
sendButton.place(x=305, y=15)
receiveButton = Button(self, image=self.img1)
receiveButton.place(x=355, y=15)
menu = Menu(self.master)
self.master.config(menu=menu)
file = Menu(menu)
file.add_command(label='Exit', command=self.client_exit)
menu.add_cascade(label='File', menu=file)
edit = Menu(menu)
edit.add_command(label='abcd')
menu.add_cascade(label='Edit', menu=edit)
help = Menu(menu)
help.add_command(label='About Us', command=self.about)
menu.add_cascade(label='Help', menu=help)
def callback():
path = filedialog.askopenfilename()
e.delete(0, END) # Remove current text in entry
e.insert(0, path) # Insert the 'path'
# print path
w = Label(root, text="File Path:")
e = Entry(root, text="")
b = Button(root, text="Browse", fg="#a1dbcd", bg="black", command=callback)
w.pack(side=TOP)
e.pack(side=TOP)
b.pack(side=TOP)
def client_exit(self):
exit()
def about(self):
top = Toplevel()
msg = Message(top, text="This is a project developed by Aditi,Sagar and
Suyash as the final year project.",
font=('', '15'))
msg.pack()
top.geometry('200x200')
button = Button(top, text="Okay", command=top.destroy)
button.pack()
top.mainloop()
root.resizable(0,0)
#size of the window
root.geometry("700x400")
app = Window(root)
root.mainloop()
Overlaying elements is tricky. I think this might be approximately what you're looking for. At least it's a start...
import PIL
from PIL import Image, ImageTk
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
root = Tk()
class Window:
def __init__(self, master=None):
tower = PIL.Image.open("Images/island.png")
master.update()
win_width = int(master.winfo_width())
win_height = int(master.winfo_height())
# Resize the image to the constraints of the root window.
tower = tower.resize((win_width, win_height))
tower_tk = ImageTk.PhotoImage(tower)
# Create a label to hold the background image.
canvas = Canvas(master, width=win_width, height=win_height)
canvas.place(x=0, y=0, anchor='nw')
canvas.create_image(0, 0, image=tower_tk, anchor='nw')
canvas.image = tower_tk
frame = Frame(master)
frame.place(x=win_width, y=win_height, anchor='se')
master.update()
w = Label(master, text="Send and receive files easily", anchor='w')
w.config(font=('times', 32))
w.place(x=0, y=0, anchor='nw')
master.title("Bifrost v1.0")
self.img1 = PhotoImage(file="Images/logo.png")
self.img2 = PhotoImage(file="Images/magnifier.png")
frame.grid_columnconfigure(0, weight=1)
sendButton = Button(frame, image=self.img2)
sendButton.grid(row=0, column=1)
sendButton.image = self.img2
receiveButton = Button(frame, image=self.img1)
receiveButton.grid(row=0, column=2)
receiveButton.image = self.img1
menu = Menu(master)
master.config(menu=menu)
file = Menu(menu)
file.add_command(label='Exit', command=self.client_exit)
menu.add_cascade(label='File', menu=file)
edit = Menu(menu)
edit.add_command(label='abcd')
menu.add_cascade(label='Edit', menu=edit)
help = Menu(menu)
help.add_command(label='About Us', command=self.about)
menu.add_cascade(label='Help', menu=help)
def callback():
path = filedialog.askopenfilename()
e.delete(0, END) # Remove current text in entry
e.insert(0, path) # Insert the 'path'
# print path
w = Label(root, text="File Path:")
e = Entry(root, text="")
b = Button(root, text="Browse", fg="#a1dbcd", bg="black", command=callback)
w.pack(side=TOP)
e.pack(side=TOP)
b.pack(side=TOP)
def client_exit(self):
exit()
def about(self):
message = "This is a project developed by Aditi,Sagar and"
message += "Suyash as the final year project."
messagebox.showinfo("Delete Theme", message)
root.resizable(0,0)
#size of the window
root.geometry("700x400")
app = Window(root)
root.mainloop()
I'm struggling with grid layout - basically I want to print some data in the loop like this:
But can't figure it out by myself. I managed to make it work properly but just for the first entry - my code:
from tkinter import *
from PIL import Image, ImageTk
import urllib.request
import io
app = Tk()
images = []
for i in range(0, 8):
text1 = Label(font=("Helvetica",10), text="top, left", cursor="hand2")
text2 = Label(font=("Helvetica",10), text="top, right")
text3 = Label(font=("Helvetica",10),text="lower")
image = "https://www.gravatar.com/avatar/b9630126afbff209bb068195307a5e4c?s=328&d=identicon&r=PG"
get_image = urllib.request.urlopen(image).read()
im1 = Image.open(io.BytesIO(get_image))
im_small = im1.resize((70, 70))
im = ImageTk.PhotoImage(im_small)
image1 = Label(app, image=im)
images.append(im)
image1.grid(rowspan=2, column=0, sticky=W)
text1.grid(row=0, column=1, sticky=W)
text2.grid(row=0, column=2, sticky=W)
text3.grid(row=1, column=1, sticky=W)
app.mainloop()
and the result:
I also tried this:
from tkinter import *
from PIL import Image, ImageTk
import urllib.request
import io
app = Tk()
images = []
for i in range(0, 8):
text1 = Label(font=("Helvetica",10), text="top, left", cursor="hand2")
text2 = Label(font=("Helvetica",10), text="top, right")
text3 = Label(font=("Helvetica",10),text="lower")
image = "https://www.gravatar.com/avatar/b9630126afbff209bb068195307a5e4c?s=328&d=identicon&r=PG"
get_image = urllib.request.urlopen(image).read()
im1 = Image.open(io.BytesIO(get_image))
im_small = im1.resize((70, 70))
im = ImageTk.PhotoImage(im_small)
image_cover = Label(app, image=im)
images.append(im)
image_cover.grid(rowspan=2, column=0, sticky=W)
text1.grid(row=i+1, column=1, sticky=W)
text2.grid(row=i+1,column=2)
text3.grid(row=i+2, column=1, sticky=W)
app.mainloop()
Since the picture should occupy two rows (let's call it 1 and 2), "top left" should be in row number 1 in column 1, "top right" in column number two, and lower in row 2.
Is this what you're looking for:
from tkinter import *
root = Tk()
root.geometry("500x500")
imgvar = PhotoImage(file="world.gif")
for i in range(5):
Label(root, image=imgvar, bg='red', bd=10, relief='groove').grid()
Label(root, text="Upper", bg='blue', bd=5, relief='groove').grid(column=1, row=i, sticky=N)
Label(root, text="Lower", bg='green', bd=5, relief='groove').grid(column=1, row=i, sticky=S)
?
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()