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)
?
Related
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.
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()
i did this mini programme to show databases to the user , i can see buttons of databases but when i press on them borders appear on canvas
from tkinter import *
from tkinter import ttk
import mysql.connector
class mainpro():
def __init__(self):#its my database settings
self.db = mysql.connector.connect(
host="localhost",
user="root",
port=3306,
passwd="1234"
)
self.mycursor = self.db.cursor()
win2 = Toplevel()#idid top level because i did tk before
# Title
win2.title('Manipulate Database')
# geometry
sizex = 1000
sizey = 700
posx = 100
posy = 100
win2.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
# style
style = ttk.Style()
style.theme_use('vista')
style.configure('TLabel', font=('Calibri', 15))
style.configure('TButton', font=('Calibri', 15, 'bold'))
# menu
menu = Menu(win2)
m1 = Menu(menu, tearoff=0)
menu.add_cascade(label='File', menu=m1)
m2 = Menu(menu, tearoff=0)
menu.add_cascade(label='DLL', menu=m2)
m3 = Menu(menu, tearoff=0)
menu.add_cascade(label='DML', menu=m3)
m4 = Menu(menu, tearoff=0)
menu.add_cascade(label='Help', menu=m4)
m1.add_command(label='Show databases', command=lambda: showdata())
win2.config(menu=menu)
def showdata():
def event(event):
canvas.config(scrollregion=canvas.bbox("all"))
self.mycursor.execute('SHOW DATABASES')
list = self.mycursor.fetchall()
canvas = Canvas(win2, width=1000, height=700)
f1 = Frame(canvas)
canvas.create_window((0, 0), window=f1, anchor='nw')
scroll = Scrollbar(win2, orient="vertical", command=canvas.yview)
scroll.pack(side="right", fill="y")
canvas.configure(yscrollcommand=scroll.set)
canvas.pack()
f1.bind("<Configure>", event)
y = 0
for x in list:
y += 1
ttk.Label(f1, text=str(y) + '-').grid(column=0, row=y, padx=10, pady=10, sticky='w')
ttk.Button(f1, text=x, width=35).grid(column=1, row=y, padx=10, pady=10, sticky='w')
win2.mainloop()
mainpro()
help please
You could try adding highlightthickness=0:
canvas = Canvas(win2, width=1000, height=700, highlightthickness=0)
Try this,
Source: Check
import tkinter # assuming Python 3 for simplicity's sake
import tkinter.ttk as ttk
root = tkinter.Tk()
f = tkinter.Frame(relief='flat')
lF = ttk.LabelFrame(root, labelwidget=f, borderwidth=4)
lF.grid()
b = ttk.Button(lF, text='')
b.grid()
root.mainloop()
Or try this
Canvas=Canvas(self,width=width/2,height=height/2,bg=bgCanvasColor,borderwidth=0, highlightthickness=0)
I don't have any idea how to make a buttons that output an integer.
This is a cash register system, everytime the user click that button the price should automatically sums up in the total, and automatically minus and show up a change.
from tkinter import *
from tkinter.font import Font
class MainUI:
def __init__(self,master):
#MAIN WINDOW
self.mainFrame = Frame(master, width=800, height=600,
bg="skyblue")
master.title("FCPC CASH REGISTER")
self.mainFrame.pack()
self.title = Label(self.mainFrame, text="FIRST CITY PROVIDENTIAL"
" COLLEGE", bg="blue")
self.font = Font(family="Helvetica", size=29)
self.title.configure(font=self.font)
self.title.place(x=50, y=50)
#BUTTONS
self.snacksButton = Button(self.mainFrame, text="SNACKS",
command=self.snackList, width=10, bg="blue")
self.font = Font(family="Helvetica", size=20)
self.snacksButton.configure(font=self.font)
self.snacksButton.place(x=100,y=150,)
#TOTAL
self.total = Label(self.mainFrame,text="TOTAL:",
bg="blue",width=8)
self.total.place(x=370,y=160)
self.font = Font(family="Helvetica", size=25)
self.total.configure(font=self.font)
#CHANGE
self.change = Label(self.mainFrame,text="CHANGE:", bg="blue")
self.change.place(x=370,y=240)
self.font = Font(family="Helvetica", size=25)
self.change.configure(font=self.font)
def snackList(self):
self.frame = Toplevel(width=800,height=600,bg="skyblue")
self.frame.title("SNACKS")
self.novaButton = Button(self.frame, text="NOVA(12php)",
command=self.calculate ,width=15, bg="blue")
self.font = Font(family="Helvetica", size=15)
self.novaButton.configure(font=self.font)
self.novaButton.place(x=100,y=150)
root = Tk()
ui = MainUI(root)
root.mainloop()
I want the button to output the price and automatically calculates the total amount.
I've put together a simple example to illustrate what you might need to do. Associate each button to a function that adds the price of a snack to the total amount and changes what is displayed. One way to do it is with a textvariable option of tkinter Label widget.
Example:
import tkinter as tk
def calculate(price):
global total, var
total += price
text = 'Total = ' + str(total)
var.set(text)
root = tk.Tk()
total = 0
var = tk.StringVar()
button1 = tk.Button(root, text='Snickers 1$', command=lambda: calculate(price=1)).pack()
button2 = tk.Button(root, text='Mars 2$', command=lambda: calculate(price=2)).pack()
totalLabel = tk.Label(root, textvariable=var).pack()
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()