How to insert image into different columns in tkinter Treeview widget? - python

I just want, that image was in specified (in column named 'Country') column instead of first. Thanks!
from tkinter import ttk
from tkinter import *
root = Tk()
s = ttk.Treeview(columns=('#1', '#2'))
s.heading('#0', text='Ip')
s.heading('#1', text='Port')
s.heading('#2', text='Country')
s.pack()
v = PhotoImage(file='uk.png')
s.insert('', 2, values=('127.0.0.1', '8888'), image=v)
root.mainloop()

I've made it simple :) just starting with country
from tkinter import ttk
from tkinter import *
root = Tk()
s = ttk.Treeview(columns=('#1', '#2'))
s.heading('#0', text='Country')
s.heading('#1', text='Ip')
s.heading('#2', text='Port')
v = PhotoImage(file='uk.png')
s.insert('', 2, values = ('127.0.0.1', '8888'), image=v)
s.pack()
root.mainloop()
If you don't like the image in the first place, I can imagine this, without treeview, though
from tkinter import ttk
from tkinter import *
root = Tk()
v = PhotoImage(file='uk.png')
# header
b = Label(root, text="Ip")
b.grid(row=0, column=0)
b = Label(root, text="port")
b.grid(row=0, column=1)
b = Label(root, text="Country")
b.grid(row=0, column=2)
lista = [["800","127.1.1.",v]]
height = 2
width = 3
for i in range(height-1): #Rows
b1 = Label(root, text=lista[i][0])
b1.grid(row=i+1, column=0)
b2 = Label(root, text=lista[i][1])
b2.grid(row=i+1, column=1)
b3 = Label(root, image=lista[i][2])
b3.grid(row=i+1, column=2)
mainloop()

Related

How should I change my tkinter code to rearrange the elements on my page?

I'm learning tkinter and getting stumped in one area. Here's the code:
from tkinter import *
from tkinter.messagebox import showinfo
def button_press():
showinfo('info','pressed button')
root = Tk()
root.geometry('800x500')
f = Frame(root)
f.pack()
Label(f, text="this is a line of text").pack(side=LEFT)
s = StringVar(value='enter here')
Entry(f, textvariable=s, width=100).pack(side=LEFT)
Button(f, text='Button', command=button_press).pack(side=RIGHT)
root.mainloop()
It produces:
But I want to align the text vertically with the entry field like this:
What do I need to change to make that happen?
Using .pack() is not advisable if you want to create more complex Frame structures.
Instead assign the items to variables and place those into a .grid().
.grid splits up your frame into different rows and columns or "sticks" them on a certain place.
below an example:
from tkinter import *
from tkinter.messagebox import showinfo
def button_press():
showinfo('info', 'pressed button')
root = Tk()
root.geometry('800x500')
f = Frame(root)
f.pack()
l1 = Label(f, text="this is a line of text")
l1.grid(row=1, column=1, sticky=W)
s = StringVar(value='enter here')
entry = Entry(f, textvariable=s, width=100)
entry.grid(row=2, column=1)
button = Button(f, text='Button', command=button_press)
button.grid(row=2, column=2)
root.mainloop()

python importing file but image not recognized

I have created one python file with a drowdown menu. When i choose the option one, it imports another python file, with a checkbutton and a picture in a canvas. Both files and the picture are located in the same folder. The code import the file imports the canvas and the checkbutton, but I get the error saying image "pyimage1" doesn't exist. If I run that second file alone, it does show the checkbutton and the image without errors. When Import a python file the images are not recognized anymore or am I doing something wrong? is any workaround there?
main program:
from tkinter import *
root = Tk()
root.geometry('1560x750')
canvas=Canvas(root)
canvas.config(width=1000, height=1560, bg='light grey')
canvas.grid(row=1,column=3, rowspan=1550,ipadx=1300,ipady=750,sticky=NW)
def option_number(x):
if x == "one":
import part2
variable = StringVar()
variable.set("options")
w = OptionMenu(canvas, variable, "one", "two",command = option_number)
w.config(width=15, height=1,bg='blue')
w.place(x=400,y=100)
root.mainloop()
file to be imported:
from tkinter import *
root = Tk()
root.geometry('1560x750')
canvas=Canvas(root)
canvas.config(width=1000, height=1560, bg='light grey')
canvas.grid(row=1,column=3, rowspan=1550,ipadx=1300,ipady=750,sticky=NW)
button = Checkbutton(canvas).place(x=170, y=230)
AND_gate=PhotoImage(file='AND.png') #set up variables for and_gate
labelimage_and = Label(canvas, image=AND_gate).place(x=200,y=200)
root.mainloop()
Updated code to import function:
from tkinter import *
root = Tk()
root.geometry('1560x750')
canvas=Canvas(root)
canvas.config(width=1000, height=1560, bg='light grey')
canvas.grid(row=1,column=3, rowspan=1550,ipadx=1300,ipady=750,sticky=NW)
def option_number(x):
if x == "one":
from part1 import import_def
variable = StringVar()
variable.set("options")
w = OptionMenu(canvas, variable, "one", "two",command = option_number)
w.config(width=15, height=1,bg='blue')
w.place(x=400,y=100)
root.mainloop()
file where is the function to be imported:
from tkinter import *
root = Tk()
def import_def():
root = Toplevel()
root.geometry('1560x750')
canvas2 = Canvas(root)
canvas2.config(width=1000, height=1560, bg='red')
canvas2.grid(row=1, column=3, rowspan=1550, ipadx=1300, ipady=750, sticky=NW)
button = Checkbutton(canvas2).place(x=170, y=230)
AND_gate=PhotoImage(file='AND.png') #set up variables for and_gate
labelimage_and = Label(canvas2, image=AND_gate).place(x=200,y=200)
root.mainloop()
This is the way that I know on how to import files and functions within tkinter, not sure if this is the right way but take a look at the changes I made to both the files
main.py:
from tkinter import *
from function import import_def
root = Tk()
root.geometry('1560x750')
canvas=Canvas(root)
canvas.config(width=1000, height=1560, bg='light grey')
canvas.grid(row=1,column=3, rowspan=1550,ipadx=1300,ipady=750,sticky=NW)
def option_number(x):
if x == "one":
import_def()
variable = StringVar()
variable.set("options")
w = OptionMenu(canvas, variable, "one", "two",command = option_number)
w.config(width=15, height=1,bg='blue')
w.place(x=400,y=100)
root.mainloop()
and function.py:
from tkinter import *
def import_def():
root = Toplevel()
root.geometry('1560x750')
canvas2 = Canvas(root)
canvas2.config(width=1000, height=1560, bg='red')
canvas2.grid(row=1, column=3, rowspan=1550, ipadx=1300, ipady=750, sticky=NW)
button = Checkbutton(canvas2).place(x=170, y=230)
AND_gate=PhotoImage(file='sad songs.jpg') #set up variables for and_gate
labelimage_and = Label(canvas2, image=AND_gate).place(x=200,y=200)
root.mainloop()
Hope it was of some help, do let me know if any doubts or errors.
Cheers

Python tkinter label formulas

how can I make a label with a formula and whenever I change the value in entry, does it automatically change the value in the label? As if it were excel cells with formulas. And in case it would have to be invisible (without content) while the entry is empty.
import tkinter as tk
root = tk.Tk()
ent1 = tk.Entry(root)
lab1 = tk.Label(root,text='Price')
ent2 = tk.Entry(root)
lab2 = tk.Label(root,text='Quantity')
lab3 = tk.Label(root,text='') #lab3 formula = float(ent1.get()) * int(ent2.get())
lab1.pack()
ent1.pack()
lab2.pack()
ent2.pack()
lab3.pack()
Use the trace method to execute a function when a variable changes.
import tkinter as tk
def update(*args):
try:
output_var.set(price_var.get() * quantity_var.get())
except (ValueError, tk.TclError):
output_var.set('invalid input')
root = tk.Tk()
lab1 = tk.Label(root,text='Price')
price_var = tk.DoubleVar()
price_var.trace('w', update) # call the update() function when the value changes
ent1 = tk.Entry(root, textvariable=price_var)
lab2 = tk.Label(root,text='Quantity')
quantity_var = tk.DoubleVar()
quantity_var.trace('w', update)
ent2 = tk.Entry(root, textvariable=quantity_var)
output_var = tk.StringVar()
lab3 = tk.Label(root, textvariable=output_var) #lab3 formula = float(ent1.get()) * int(ent2.get())
lab1.pack()
ent1.pack()
lab2.pack()
ent2.pack()
lab3.pack()
root.mainloop()

Add text entry widget over picture in tkinter using python

How can I make a text entry widget over picture in tkinter using python?
The code I am using:
from tkinter import *
from PIL import ImageTk, Image
import os
root = Tk()
root.title('Title')
E1 = Entry(root, bd = 5,show='*') # tried this line of code but it didn't worked
img = ImageTk.PhotoImage(Image.open("path/to/image.png"))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
c = Button(text=" OK ")
c.place(relx=0.95, rely=0.98, anchor=SE)
root.mainloop()
It can make a button (OK) , why it can't make entry text widget?
You forgot to pack the Entry field, just try something like:
E1 = Entry(root, bd = 5,show='*')
E1.pack(side=BOTTOM, fill=BOTH, expand=YES)
And customize the position and behaviour as you prefer.

Why don't any of my buttons work?

First i had the sqrt button which worked fine, and then I added the pi button, and nothing worked, i tried changing everything and i still don't know whats wrong! Please someone help.
import sys
from tkinter import *
from math import *
def sqrt_():
text = ment.get()
a = sqrt(text)
label['text'] = a
def pi_():
text = ment.get()
a = pi(text)
label_1['text'] = a
root = Tk()
root.title('Conversions')
root.geometry('400x400')
#Get square root
sqrt_button = Button(root, text='Get Square root',command= sqrt_).place(x='160', y='5')
label = Label(root, text='')
label.place(x=5, y=30)
ment = IntVar()
entry = Entry(textvariable=ment).place(x='5', y= '10 ')
#Get Pi
pi_button = Button(root, text='Get Pi',command= pi_).place(x='160', y='50')
label_1 = Label(root, text='')
label_1.place(x=55, y=200)
ment = IntVar()
entry_1 = Entry(textvariable=ment).place(x='5', y= '55 ')
root.mainloop()
First, you don't define the function pi which means when you click the second button it will fail.
Second, you redefined the ment. In this case both two entries will be bound to the same int. This means when you click the first button, it will read the value from the second entry. So change all the second ment to ment_1. The name, the name in entry and the name in pi_.
import sys
from tkinter import *
from math import *
def sqrt_():
text = ment.get()
a = sqrt(text)
label['text'] = a
def pi_():
label_1['text'] = pi
root = Tk()
root.title('Conversions')
root.geometry('400x400')
#Get square root
sqrt_button = Button(root, text='Get Square root',command= sqrt_).place(x='160', y='5')
label = Label(root, text='')
label.place(x=5, y=30)
ment = IntVar()
entry = Entry(textvariable=ment).place(x='5', y= '10 ')
#Get Pi
pi_button = Button(root, text='Get Pi',command= pi_).place(x='160', y='50')
label_1 = Label(root, text='')
label_1.place(x=55, y=200)
ment_1 = IntVar()
entry_1 = Entry(textvariable=ment_1).place(x='5', y= '55 ')
root.mainloop()
pi isn't a function it is a constant so:
def pi_():
label_1['text'] = pi

Categories

Resources