I want to create a 2 by 2 box in python's tkinter, which will be my "world".
Is there a way to set the X and Y axes on the "world"?
Something like:
setXscale(-1.0, +1.0);
setYscale(-1.0, +1.0);
This can be done with the .pack() method as can be seen below:
from tkinter import *
root = Tk()
top = Frame(root)
bottom = Frame(root)
topleft = Frame(top)
topright = Frame(top)
bottomleft = Frame(bottom)
bottomright = Frame(bottom)
lbl1 = Label(topleft, text="topleft")
lbl2 = Label(topright, text="topright")
lbl3 = Label(bottomleft, text="bottomleft")
lbl4 = Label(bottomright, text="bottomright")
top.pack(side="top")
bottom.pack(side="bottom")
topleft.pack(side="left")
topright.pack(side="right")
bottomleft.pack(side="left")
bottomright.pack(side="right")
lbl1.pack()
lbl2.pack()
lbl3.pack()
lbl4.pack()
root.mainloop()
This creates a top frame and a bottom frame, each of which contain a left and right frame.
These frames are then packed in their respective side.
Alternatively, this can be done a lot easier with .grid() like this:
from tkinter import *
root = Tk()
topleft = Frame(root)
topright = Frame(root)
bottomleft = Frame(root)
bottomright = Frame(root)
lbl1 = Label(topleft, text="topleft")
lbl2 = Label(topright, text="topright")
lbl3 = Label(bottomleft, text="bottomleft")
lbl4 = Label(bottomright, text="bottomright")
topleft.grid(row = 0, column = 0)
topright.grid(row = 0, column = 1)
bottomleft.grid(row = 1, column = 0)
bottomright.grid(row = 1, column = 1)
lbl1.grid(row = 0, column = 0)
lbl2.grid(row = 0, column = 0)
lbl3.grid(row = 0, column = 0)
lbl4.grid(row = 0, column = 0)
root.mainloop()
Or like this:
from tkinter import *
root = Tk()
lbl1 = Label(root, text="topleft")
lbl2 = Label(root, text="topright")
lbl3 = Label(root, text="bottomleft")
lbl4 = Label(root, text="bottomright")
lbl1.grid(row = 0, column = 0)
lbl2.grid(row = 0, column = 1)
lbl3.grid(row = 1, column = 0)
lbl4.grid(row = 1, column = 1)
root.mainloop()
Related
I am making an F1 drivers' application, which gathers API data. The application has designated labels for where specific API content needs to be placed.
What's happening is that whenever I put anything beside a designated label through .grid, the distance between whatever I put and the label is too big. A perfect example of this is in the bottom_left_frame. I've tried different ways (i.e, sticky, padding), but no luck. One way that seems to work is .configure(), but I don't think it's a scalable solution, and I want to place my objects through .grid()
Here's the code. Would appreciate the help!
#import necessary modules
from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image
from tkinter import messagebox
import requests
import json
root = Tk()
root.title("F1 Desktop Application")
root.geometry("500x600")
root.configure(bg="white")
#NEED TO UNDERSTAND PRECEDENCE BETWEEN WIDTH, HEIGHT SETTING, VS SPANS. STICKY MAKES PERFECT SENSE
top_frame = LabelFrame(root, width = 300, height = 125)
top_frame.grid(row = 0, column = 0, columnspan = 2)
top_frame.rowconfigure(0, minsize = 75)
top_frame.columnconfigure(0, minsize = 300)
header_label = Label(top_frame, text = "F1 2022 Drivers App", font = ("Arial bold",14), bg = "white")
header_label.grid(row = 0, column = 0)
entry= Entry(top_frame)
entry.grid(row = 1, column = 0)
#search_button = Button(top_frame, text = "search")
#search_button.grid(row = 1, column = 1)
left_frame = LabelFrame(root, width = 250, height = 225, borderwidth = 0, highlightthickness = 2)
left_frame.grid(row = 1, column = 0, sticky = NW, padx = 10, pady = 15)
#left_frame.grid_propagate(False)
left_frame.rowconfigure(0, minsize = 100)
left_frame.columnconfigure(0, minsize = 200)
#left_frame_content = LabelFrame(, width = 125, height = 70)
#left_frame_content.grid(row = 1, column = 1, sticky = W)
basic_info = Label(left_frame, text = "Basic Info ", font = ("Arial bold",14))
basic_info.grid(row = 0, column = 0)
full_name = Label(left_frame, text = "Full Name :")
full_name.grid(row = 1, column = 0, sticky = W)
driver_code = Label(left_frame, text = "Driver Code : ")
driver_code.grid(row = 2, column = 0, sticky = W)
nationality = Label(left_frame, text = "Nationality : ")
nationality.grid(row = 3, column = 0, sticky = W)
bottom_left_frame = LabelFrame(root, width = 250, height = 225)
bottom_left_frame.grid(row = 2, column = 0, sticky = N, padx = 10)
bottom_left_frame.rowconfigure(0, minsize = 25)
bottom_left_frame.columnconfigure(0, minsize = 200)
F1_career = Label(bottom_left_frame, text = "F1 Career ", font = ("Arial bold",14))
F1_career.grid(row = 0, column = 0)
wins = Label(bottom_left_frame, text = "Wins :")
wins.grid(row = 1, column = 0, sticky = W)
wins.configure(text = "Wins :" + " 7")
poles = Label(bottom_left_frame, text = "Poles :")
poles.grid(row = 2, column = 0, sticky = W)
test = Label(bottom_left_frame, text = "test")
test.grid(row = 2, column = 1)
podiums = Label(bottom_left_frame, text = "Podiums :")
podiums.grid(row = 3, column = 0, sticky = W)
drivers_championships = Label(bottom_left_frame, text = "Championships :")
drivers_championships.grid(row = 4, column = 0, sticky = W)
bottom_right_frame = LabelFrame(root, width = 250, height = 225)
bottom_right_frame.grid(row = 2, column = 1, sticky = W)
bottom_left_frame.rowconfigure(0, minsize = 50)
bottom_left_frame.columnconfigure(0, minsize = 150)
root.mainloop()
This is my first venture into tkinter programming, so far I have the following code that increases or decreases a number by pressing either button. As you may notice I have started adding an update definition that I'd like to update a results table with the label value each time a button is pressed. I've recently found the lambda expression to add two commands to each button press but can't find an example to build the list:
import tkinter as tk
window = tk.Tk()
def increase():
value = int(lbl_value["text"])
lbl_value["text"] = f"{value + 1}"
def decrease():
value = int(lbl_value["text"])
lbl_value["text"] = f"{value - 1}"
def update():
result_table = []
window.rowconfigure(0, minsize = 100, weight = 1)
window.columnconfigure([0,1,2], minsize = 100, weight = 1)
btn_decrease = tk.Button(master = window, text = "-", command = lambda:[decrease(), update()], bg = 'red', fg = 'white')
btn_decrease.grid(row = 0, column = 0, sticky = "nsew")
lbl_value = tk.Label(master = window, text = "0")
lbl_value.grid(row = 0, column = 1)
btn_increase = tk.Button(master = window, text = "+", command = lambda:[increase(), update()], bg = 'green', fg = 'white')
btn_increase.grid(row = 0, column = 2, sticky = "nsew")
window.mainloop()
, bg = 'black', fg = 'white')
btn_decrease.grid(row = 0, column = 0, sticky = "nsew")
lbl_value = tk.Label(master = window, text = "0")
lbl_value.grid(row = 0, column = 1)
btn_increase = tk.Button(master = window, text = "+", command = increase, bg = 'red', fg = 'white')
btn_increase.grid(row = 0, column = 2, sticky = "nsew")
window.mainloop()
I'd like to add a graph of the count to the display ultimately. Any help greatly appreciated.
Matt
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.title("Images")
root.iconbitmap(r"C:\Users\DellABD\Downloads\lolol.ico")
my_img1 = ImageTk.PhotoImage(Image.open(r"E:\imagespy\illusion.jpg"))
my_img2 = ImageTk.PhotoImage(Image.open(r"E:\imagespy\scan0053.jpg"))
my_img3 = ImageTk.PhotoImage(Image.open(r"E:\imagespy\scan0054.jpg"))
my_img4 = ImageTk.PhotoImage(Image.open(r"E:\imagespy\scan0055.jpg"))
my_img5 = ImageTk.PhotoImage(Image.open(r"E:\imagespy\scan0056.jpg"))
my_img_list = [my_img1, my_img2, my_img3, my_img4, my_img5]
my_label = Label(image = my_img1)
my_label.grid(row = 0, column = 0, columnspan = 3)
def forward(image_number):
global my_label
global back
global forward
my_label.grid_forget()
my_label = Label(my_img_list[image_number - 1])
my_label.grid(row = 0, column = 0, columnspan = 3)
def back():
global my_label
global back
global forward
global my_label
global back
global forward
my_label.grid_forget()
my_label = Label(my_img_list[image_number - 1])
button_forward = Button(root, text = ">>>", command = lambda: image_number + 1)
button_back = Button(root, text = "<<<", command = lambda: image_number - 1)
if button_back == 1:
button_back = Buuton(rrot, text = "<<<", state = DISABLED)
button_back.grid(row = 1, column = 0)
button_forward.grid(row = 1, column = 2)
my_label.grid(row = 0, column = 0, columnspan = 3)
button_back = Button(root, text = "<<<", padx = 20, pady = 20, command = back)
button_forward = Button(root, text = ">>>", padx = 20, pady = 20, command = lambda: forward(2))
button_quit = Button(root, text = "Exit", padx = 20, pady = 20, command = root.quit)
button_back.grid(row = 1, column = 0)
button_forward.grid(row = 1, column = 2)
button_quit.grid(row = 1, column = 1)
root.mainloop()
when I run the programme and press the Forward button it shows me an error. I wanted to move on next image by pressing forward and when I press the first image disappears but next image dont show up and shows error.
if master._last_child_ids is None:
AttributeError: 'PhotoImage' object has no attribute '_last_child_ids'
The problem is this code:
Label(my_img_list[image_number - 1])
You are trying to use an image as the parent/master of a label. You need to use this instead:
Label(image=my_img_list[image_number - 1])
I want to make a button in my widget that when I press it, it proceeds to the next lines of code, and closes the existing widget of where the button is.
from tkinter import *
root = Tk()
Label(root, text = "Childs First name").grid(row = 0, sticky = W)
Label(root, text = "Childs Surname").grid(row = 1, sticky = W)
Label(root, text = "Childs Year of Birth").grid(row = 2, sticky = W)
Label(root, text = "Childs Month of Birth").grid(row = 3, sticky = W)
Label(root, text = "Childs Day of Birth").grid(row = 4, sticky = W)
Fname = Entry(root)
Sname = Entry(root)
x = Entry(root)
y = Entry(root)
z = Entry(root)
Fname.grid(row = 0, column = 1)
Sname.grid(row = 1, column = 1)
x.grid(row = 3, column = 1)
y.grid(row = 2, column = 1)
z.grid(row = 4, column = 1)
button1 = Button(root, text = "Quit", command = root.quit, bg = "Grey", fg = "White", width = 12).grid(row = 5, column = 0, sticky = W)
def save():
Fname_value = Fname.get()
Sname_value = Sname.get()
x_value = x.get()
y_value = y.get()
z_value = z.get()
save()
mainloop()
Sorry for this late answer. To make a 'continue' button:
continue_button = tk.Button(frame,text='continue',command=func)
continue_button.config(width=width_continue_button)
# set the coordinates as you want. here 2,6 for the example
continue_button.grid(row=2,column=6,padx=width_continue_grid)
then you have to write the function 'func' to fulfill your needs.
hope this helps
When i put widgets inside my canvas, the widgets bypass the canvas size. But when I scroll, the canvas won't move.
On top of that, the canvas auto re-sizes when I add more widgets. I thought the canvas isn't supposed to re-size because I already added a scroll bar?
Here's my code:
from tkinter import *
from tkinter import ttk
import mysql.connector
class CanvasScroll:
def on_resize(self,event):
self.canvas.config(width = 1185, height = 530)
def add_crew(self):
crewFrame = CrewFrame()
def callback_list(self):
index = self.notebook.index(self.notebook.select()) + 1
self.tmpframe = Try2(self.contentframe, index)
def __init__(self, master):
self.canvas = Canvas(master, width = 1185, height = 530, scrollregion = (0, 0, 1216, 700), bg = 'white', confine = True)
self.canvas.grid(row = 2, column = 0, sticky = 'news')
self.xscroll = ttk.Scrollbar(master, orient = HORIZONTAL, command = self.canvas.xview)
self.xscroll.grid(row = 3, column = 0, sticky = 'we')
self.yscroll = ttk.Scrollbar(master, orient = VERTICAL, command = self.canvas.yview)
self.yscroll.grid(row = 2, column = 1, sticky = 'ns')
self.canvas.config(xscrollcommand = self.xscroll.set, yscrollcommand = self.yscroll.set)
self.canvas.bind("<Configure>", self.on_resize)
self.option = ttk.Frame(master, height = 150, width = 1206)
self.option.grid(row = 0, column = 0, sticky = 'we', columnspan = 5)
self.addicon = PhotoImage(file = 'C:\\Users\\rain\\Desktop\\Fyosh!\\logo\\add.gif').subsample(2,2)
self.btnAdd = ttk.Button(self.option, image = self.addicon, text = 'Add Crew', compound = TOP, command = self.add_crew)
self.btnAdd.grid(row = 0, column = 0, padx = 50, pady = 5)
self.updateicon = PhotoImage(file = 'C:\\Users\\rain\\Desktop\\Fyosh!\\logo\\update.gif').subsample(2,2)
self.btnUpdate = ttk.Button(self.option, image = self.updateicon, text = 'Update Crew', compound = TOP)
self.btnUpdate.grid(row = 0, column = 1, padx = 50)
self.deleteicon = PhotoImage(file = 'C:\\Users\\rain\\Desktop\\Fyosh!\\logo\\delete.gif').subsample(2,2)
self.btnDelete = ttk.Button(self.option, image = self.deleteicon, text = 'Delete Crew', compound = TOP)
self.btnDelete.grid(row = 0, column = 2, padx = 50)
self.reloadicon = PhotoImage(file = 'C:\\Users\\rain\\Desktop\\Fyosh!\\logo\\Refresh.png').subsample(7,7)
self.reloadbtn = ttk.Button(self.option, image = self.reloadicon, command = self.callback_list, text = 'Load List', compound = TOP)
self.reloadbtn.grid(row = 0, column = 3, padx = 50)
self.tabframe = ttk.Frame(master, height = 20)
self.tabframe.grid(row = 1, sticky = 'we')
self.notebook = ttk.Notebook(self.tabframe)
self.notebook.grid(row = 0, column = 0, sticky = 'we')
db = mysql.connector.connect(user = 'root', password = '', host = 'localhost', database = 'fat2x_payroll')
cursor = db.cursor()
ships = ("SELECT * from tbl_shiplist")
try:
cursor.execute(ships)
result = cursor.fetchall()
self.tab = {}
for row in result:
self.tab[row[0]] = {
"shipname": ttk.Frame(self.notebook)
}
self.notebook.add(self.tab[row[0]]["shipname"], text = row[1])
db.close()
except:
messagebox.showinfo("alert", "ship list error")
self.canvas.label = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 0)
self.canvas.label1 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 1)
self.canvas.label2 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 2)
self.canvas.label3 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 3)
self.canvas.label4 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 4)
self.canvas.label5 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 5)
self.canvas.label6 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 6)
self.canvas.label7 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 7)
self.canvas.label8 = ttk.Label(self.canvas, text = "hahahahahha").grid(column = 8)
def start():
master = Tk()
master.geometry("1206x690+10+10")
master.resizable(0,0)
master.title("Crew Panel")
canvasScroll = CanvasScroll(master)
if __name__ == "__main__":
start()
The canvas can only scroll objects that are embedded on the canvas. In the case of widgets, that means that you've called the create_window method of the canvas for the widgets you want to be affected by the scrollbar.
If you want to use grid to organize widgets, and you want those widgets to be in a scrollable container, the usual method is to put those widgets inside a frame, and then add the frame to the canvas with create_window. You can see an example of this technique in this answer: https://stackoverflow.com/a/3092341/7432
As for the auto-resize, this behavior is completely unrelated to using a scrollbar. If you use grid or pack on a widget, the parent will always resize unless you've turned geometry propagation off. It doesn't matter if it's a Canvas or a Frame or a Toplevel, or any other class of widget.