How can I fix my button positions on python tkinter? - python-3.x

I want to fix the position of my two buttons like if I resize my window the buttons will remain in their positions. Or My buttons dynamically resize with the window size. I have positioned my buttons using place. But I ain't getting success. Here's the code so far.
import turtle
import tkinter as tk
##wn=turtle.Screen()
wn=tk.Tk()
image=tk.PhotoImage(file="MS-3.PNG")
wn.geometry("700x700")
label1=tk.Label(wn,image=image)
label1.pack(side="top",fill="both",expand="yes")
label1.grid_rowconfigure(0, weight=1)
label1.grid_columnconfigure(0, weight=1)
label1.grid_rowconfigure(1, weight=1)
def callback1():
import Detection1
def callback():
import detection
button1=tk.Button(label1,text="Health Identification", command=callback, bd=12,bg="grey",font="caliber")
button1.place(x=100,y=500 )
label1.image=image
button2=tk.Button(label1,text="Disease Classification", command=callback1, bd=10,bg="grey",font="caliber")
button2.place(x=400, y=500 )
label1.image=image
wn.mainloop()

Sadia: If I understood your question correctly, you could make your windows non-resizable with wn.resizable(False, False). Then place your buttons exactly where you want them to be. If you are new to tkinter and python, making every object resizable might be a bit too complex to begin with.
Hopefully this helps.
import turtle
import tkinter as tk
from PIL import Image, ImageTk
##wn=turtle.Screen()
wn = tk.Tk()
wn.geometry("700x700")
wn.resizable(False, False)
img = ImageTk.PhotoImage(Image.open("MS-3.PNG"))
label1 = tk.Label(wn, image=img)
label1.pack(side="top", fill="both", expand="yes")
# label1.grid_rowconfigure(0, weight=1)
# label1.grid_columnconfigure(0, weight=1)
# label1.grid_rowconfigure(1, weight=1)
def callback1():
import Detection1
def callback():
import detection
button1 = tk.Button(label1, text="Health Identification", command=callback, bd=12, bg="grey", font="caliber")
button1.place(x=100, y=500)
button2 = tk.Button(label1, text="Disease Classification", command=callback1, bd=10, bg="grey", font="caliber")
button2.place(x=400, y=500)
wn.mainloop()

Related

Why my ttk style won't apply to my Frame?

I've been fighting with ttk style for about 2 days right now, but nothing seems to work. I've tried a lot of different approaches, but I had no effect. Right now, I just want to see that the Frame that I'm trying to add to my App is working, could you please show me where is the problem?
import tkinter as tk
from tkinter import ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
# Main window configuration
self.title("Work Tracker")
self.geometry("600x700")
self.resizable(0,0)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
# Styles
style = ttk.Style()
style.configure('TFrame', background='blue')
# Logo frame
frame = ttk.Frame(self, width=200, height=150)
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_propagate(False)
frame.grid(row=0, column=0, sticky='nw')
label = ttk.Label(frame, text ="WORK TRACKER")
label.pack()
def main():
app = App()
app.mainloop()
main()

Tkinter Grid Geometry Resizing issue

Browse Button in Right Hand side Panel is being separated when resizing the window. I want the whole thing to stay together and resizing equally.
import tkinter as tk
from tkinter import ttk
from tkinter import *
class AppLayout(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.masterPane = tk.PanedWindow(self )
self.leftPane = tk.Frame(self.masterPane,relief = 'raised',bg='black',width =100)
self.masterPane.add(self.leftPane)
self.rightPane = tk.Frame(self.masterPane)
self.masterPane.add(self.rightPane)
self.masterPane.pack(fill = 'both',expand = True)
name_entry = tk.Entry(self.rightPane,font =('calibre',10,'normal'))
Browse_Button = tk.Button(self.rightPane,text = 'Browse')
Upload_Button = tk.Button(self.rightPane,text = 'Upload')
name_entry.grid(row=1,column=1)
Browse_Button.grid(row=1,column=2)
Upload_Button.grid(row=1,column=1,pady =(50,0))
self.rightPane.columnconfigure(1, weight=1)
self.rightPane.rowconfigure(1, weight=1)
app = AppLayout()
app.mainloop()
There are four things you need to change:
don't double-import tkinter. It's enough to import * from it.
configure the second column of the rightPane to extend when you resize the window.
Put the button at the western side (W) of the second column by adding sticky=W to the .grid() method.
Glue the Entry widget to the Eastern and Western side of column 1. So, it will get wider when this column extends.
With the following code, it works.
You can also use the .colmnconfigure() and .rowconfigure() method on frame widgets to specify how the other pane extends and how the app resizes vertically.
from tkinter import ttk
from tkinter import *
class AppLayout(Tk):
def __init__(self):
Tk.__init__(self)
self.masterPane = PanedWindow(self )
self.leftPane = Frame(self.masterPane,relief = 'raised',bg='black',width =100)
self.masterPane.add(self.leftPane)
self.rightPane = Frame(self.masterPane)
self.rightPane.columnconfigure(2, weight=1)
self.masterPane.add(self.rightPane)
self.masterPane.pack(fill = 'both',expand = True)
name_entry = Entry(self.rightPane,font =('calibre',10,'normal'))
Browse_Button = Button(self.rightPane,text = 'Browse')
Upload_Button = Button(self.rightPane,text = 'Upload')
name_entry.grid(row=1,column=1,sticky=W+E)
Browse_Button.grid(row=1,column=2, sticky=W)
Upload_Button.grid(row=1,column=1,pady =(50,0))
self.rightPane.columnconfigure(1, weight=1)
self.rightPane.rowconfigure(1, weight=1)
app = AppLayout()
app.mainloop()

Treeview Image not displaying

having problems displaying an image using treeview. Found some other mentions on the net with a similar problem but the answers do not seem to work for me. I am using Win10 if that makes a difference
import ttkthemes
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo
from tkinter import PhotoImage
from PIL import Image, ImageTk
self = tk.Tk()
self.title('Tkinter PhotoImage Demo')
#self.image = Image.open("A4.bmp")
#self.python_image = ImageTk.PhotoImage(self.image)
#print(self.python_image)
#ttk.Label(self, image=self.python_image).pack()
# columns
columns = ('#1', '#2')
tree = ttk.Treeview(self, columns=columns, show='headings', height=20)
tree.tag_configure('oddrow', background='#ece0cf')
tree.tag_configure('evenrow', background='#e0e0e0')
style = ttk.Style()
style.theme_use('clam')
style.configure("Treeview",font=(None,12))
style.configure("Treeview.Heading", font=(None, 12))
# define headings
tree.heading('#1', text='Date')
tree.column("#1", minwidth=0, width=160)
tree.heading('#2', text='Logo')
tree.column("#2", minwidth=0, width=80)
# add a scrollbar
scrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL, command=tree.yview)
tree.configure(yscroll=scrollbar.set)
scrollbar.grid(row=0, column=1, sticky='ns')
tree.grid(row=0, column=0, sticky='nsew')
logo="A4.bmp"
rowcol='oddrow'
im = Image.open(logo)
ph = ImageTk.PhotoImage(im)
print(ph)
tree.insert('', -1, values=("2021-03-25", logo), image=ph, tags=(rowcol,))
tree.grid(row=0, column=0, sticky='nsew')
self.update_idletasks()
self.update()
If it helps the value of ph is: pyimage1
The commented out code at the start displays the image just fine, so the issue seems to be on adding the image itself into the tree.insert.... bit of coding
Whilst I have your attention and as related but very minor questions, is there also a way to add a second image into treeview? Plus is there a way to display some values, then an image, then some more values, then another image and finally more values, or do the images have to be at the start or end of the rows?

Is there a ttk equivalent of Scrolledtext widget Tkinter

I set the theme of my main window to an awtheme 'awdark'.All the widgets with ttk extension set its appearance according to the theme by itself all except for the scrolled text widget which appears white in colour(i.e, color of the field as well as the color and look of the scrollbar)probably because its not a part of ttk.My scrolledtext widget is contained in a ttk.Frame widget by the way.Is there any workaround this?
Is there a ttk equivalent of Scrolledtext widget Tkinter
No, there is not. The ttk widgets don't have a text widget.
The scrolledtext widget is just a text widget and scrollbars, there's not much more to it. You can create your own which uses ttk scrollbars with just a few lines of code.
Here's a solution that doesn't use classes. One that is class-based is just a couple extra lines of code.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
frame = ttk.Frame(root)
frame.pack(fill="both", expand=True)
text = tk.Text(frame, wrap="none")
vsb = ttk.Scrollbar(frame, command=text.yview, orient="vertical")
hsb = ttk.Scrollbar(frame, command=text.xview, orient="horizontal")
text.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set)
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
vsb.grid(row=0, column=1, sticky="ns")
hsb.grid(row=1, column=0, sticky="ew")
text.grid(row=0, column=0, sticky="nsew")
root.mainloop()

darken the whole tkinter window

I'm programming a game sa my graduation project in school. Once the manager closes the game, I want the other users to have a pop up that the game is ended and a button to click on in order to continue.
I want the whole window (with all the widgets) to get darker (not totally black but darker). Everything besides the pop up and the "continue" button
Enyone has a clue what I can do? I searched on the Internet and found nothing :/
Not able to use ImageGrab on linux, here is my solution:
import tkinter as tk
import numpy as np
import time
import pyscreenshot as ImageGrab
from PIL import Image, ImageTk
def grab(widget):
box = (widget.winfo_rootx(), widget.winfo_rooty(), widget.winfo_rootx()+widget.winfo_width(), widget.winfo_rooty()+widget.winfo_height())
grab = ImageGrab.grab(bbox=box)
return grab
def darken(widget):
widget.overlay = ImageTk.PhotoImage(Image.fromarray(np.asanyarray(grab(widget))//2))
cvs = tk.Canvas(widget, width=widget.winfo_width(), height=widget.winfo_height())
cvs.place(x=0,y=0)
cvs.create_image(0, 0, anchor='nw', image=widget.overlay)
return cvs
root = tk.Tk()
def apply_dark():
cvs = darken(root)
b = tk.Button(root, text='Ok')
def d():
cvs.destroy()
b.destroy()
b.config(command=d)
b.place(x=50, y=10)
tk.Label(root, text='Label1').grid(row=0, column=0)
tk.Label(root, text='Label2').grid(row=0, column=1)
tk.Button(root, text='Button1').grid(row=1, column=0)
tk.Button(root, text='Darken', command=apply_dark).grid(row=1, column=1)
root.mainloop()
Fiddle around with this code. Of course this is in fact just a quick bodge (non-reliable, inefficient), but still - works for me.
Hope that's helpful!

Resources