When trying to position elements in tkinter all elements are moved - python

I'm using tkinter to create a very simple GUI just to start learning how to use the module. However I'm trying to position two elements (a button and a text box). To position the elements I'm using the grid function, however I have used grid for both the button and the textbox and it seems that both elements are affected by just one set of the parameters when I have two sets of parameters for positioning.
This is the code:
from tkinter import *
import tkinter as tk
from tkinter import ttk
gui = Tk()
gui.geometry("600x800")
button = ttk.Button(text="button")
button.grid(row=1, column=1,ipady=30, ipadx=30)
lowerframe = tk.Frame(gui)
lowerframe.grid(row=2, column=1, padx = 100)
tb = ttk.Entry(lowerframe, width= 20)
tb.grid(ipady=30, ipadx= 0)
gui.mainloop()
As you can see there are two different sets of parameters for the positioning of the button and the textbox however for some reason the button and the textbox end up being in the same position. So how can I make the parameters affect the elements they're intended to affect?

Does this help? You used too many namespace tkinter. I added gui widget for Button I also added tb.grid for row and column.
Code:
import tkinter as tk
from tkinter import ttk
gui = tk.Tk()
gui.geometry("600x800")
button = ttk.Button(gui, text="button")
button.grid(row=1, column=1,ipady=30, ipadx=30)
lowerframe = ttk.Frame(gui)
lowerframe.grid(row=2, column=1, padx = 100)
tb = ttk.Entry(lowerframe, width= 20)
tb.grid(row=3, column=1, ipady=30, ipadx= 30)
gui.mainloop()
Result:

Related

Buttons not being placed inside the correct frame in tkinter

I am a newbie trying to use tkinter to build a GUI for an application. So far, I have a frame that I'd like to put several buttons into. However, every time I attempt to position this button, it isn't placed properly, being put outside of the frame itself. I wouldn't like to use the place function because of the several buttons I have to dynamically generate coming from an excel sheet so I was hoping to use the grid function instead.
Here is what I have so far
from tkinter import *
from customtkinter import *
window = Tk()
window.geometry("1920x1080")
window.state("zoomed")
window.title("My Company's Description Printer")
main_frame = CTkFrame(window, width=1920, height=1080, fg_color="grey21")
main_frame.place(x=0, y=0)
title = Label(main_frame,
text="My Company",
bg="grey21",
fg="white",
font=("Trajan Pro", 20)).place(x=626, y=30)
button_frame = CTkCanvas(main_frame,
width=800,
height=600,
highlightthickness=3,
highlightbackground="black",
relief="ridge",
bg="grey19").place(x=60, y=110)
test_button = CTkButton(button_frame, text="test").grid(row=0, column=0)
window.mainloop()
Example of code being ran
As you can see, the button is being placed in the top left corner of the entire window rather than the top left corner of the black bordered button frame. Any help would be appreciated. Thank you so much.
Note that button_frame is None because it is the result of .place(...), so the button (test_button is None as well due to same reason) is a child of the root window instead of the instance of CTkCanvas. .place(...) should be called in separate line.
Also .create_window() is used instead of tkinter layout manager to put widget into a canvas:
...
button_frame = CTkCanvas(main_frame,
width=800,
height=600,
highlightthickness=3,
highlightbackground="black",
relief="ridge",
bg="grey19")
# call .place(...) in separate line
button_frame.place(x=60, y=110)
test_button = CTkButton(button_frame, text="test") # don't use .grid(row=0, column=0)
# use .create_window() to put widget into canvas
button_frame.create_window(0, 0, window=test_button, anchor="nw")

I wanna add a small buttons bar down the note and when i do buttons dont appear why?

am trying to make a notepad app and I wanted to add a small bar at the end with buttons to do custom stuff but when I try to add a button it doesn't appear on the program what did I do wrong?
here is the script
from gc import callbacks
import re
from tkinter import *
from tkinter import ttk
import time
from turtle import right
root = Tk()
root.title('Notepad')
root.iconbitmap('C:/notes.ico')
root.geometry("500x500")
root.tk.call("source", "C:/Users/Hero/Documents/Visual Studio code/My project/azure.tcl")
root.tk.call("set_theme", "dark")
def change_theme():
if root.tk.call("ttk::style", "theme", "use") == "azure-dark":
root.tk.call("set_theme", "light")
else:
root.tk.call("set_theme", "dark")
style=ttk.Style()
style.theme_use('azure-dark')
style.configure("Vertical.TScrollbar", background="grey", bordercolor="black", arrowcolor="white")
scroll = ttk.Scrollbar(root, orient='vertical')
scroll.pack(side=RIGHT, fill='y')
text=Text(root, font=("Georgia, 24"), yscrollcommand=scroll.set, bg='#292929')
scroll.config(command=text.yview)
text.pack()
#button am talking about
fonts = ttk.Button(root, text='Size and Font', style='Accent.TButton')
fonts.pack()
root.mainloop()
Update: It worked when i had the buttons at the top but at the bottom it doesn't show up
The text box is too tall for a window with size 500x500, so the button below the text box is out of the viewable area of the window.
You can set the width and height options of the text box to a smaller values and use text.pack(fill='both', expand=1)` to expand the text box to fill the window:
# set width and height to smaller values
text = Text(root, font=("Georgia, 24"), yscrollcommand=scroll.set, bg='#292929', width=1, height=1)
# then expand it to fill the window
text.pack(fill='both', expand=1)
i fixed by adding a frame then making it at the bottom then putting the button at this frame thanks everyone that tried to help

tkinter - How can I place this button on the bottom in the middle?

I would like to place the button boutonexcel at the bottom of the window, in the middle (below both frames). I tried many combination of side=... and anchor=... without success.
How could I place it as I wish?
import os
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
fenetre_choice_of_mat=Tk()
fenetre_choice_of_mat.geometry("350x500")
class Checkbuttongroup:
def __init__(self, fenetre1, text1):
self.Checkbutton1 = IntVar()
self.texte=text1
self.fenetre12=fenetre1
Button1 = Checkbutton(self.fenetre12, text = self.texte,
variable = self.Checkbutton1,
onvalue = 1,
offvalue = 0,
height = 2,
width = 20,
anchor = "w")
Button1.pack()
cadre_choice_material_1=Frame(fenetre_choice_of_mat,highlightbackground="blue", highlightthickness=2)
cadre_choice_material_1.pack(side='left', anchor=N)
cadre_choice_material_2=Frame(fenetre_choice_of_mat,highlightbackground="blue", highlightthickness=2)
cadre_choice_material_2.pack(side='left',anchor=N)
grp_material_1=Checkbuttongroup(cadre_choice_material_1,"CW")
grp_material_2=Checkbuttongroup(cadre_choice_material_1,"CS")
grp_material_4=Checkbuttongroup(cadre_choice_material_1,"AE")
grp_material_6=Checkbuttongroup(cadre_choice_material_1,"CF")
grp_material_7=Checkbuttongroup(cadre_choice_material_1,"SE")
grp_material_3=Checkbuttongroup(cadre_choice_material_1,"RCC")
grp_material_5=Checkbuttongroup(cadre_choice_material_1,"CD")
grp_material_8=Checkbuttongroup(cadre_choice_material_2,"FS")
grp_material_9=Checkbuttongroup(cadre_choice_material_2,"COP")
grp_material_11=Checkbuttongroup(cadre_choice_material_2,"CR")
grp_material_12=Checkbuttongroup(cadre_choice_material_2,"AB")
grp_material_13=Checkbuttongroup(cadre_choice_material_2,"CT")
grp_material_14=Checkbuttongroup(cadre_choice_material_2,"LP")
grp_material_10=Checkbuttongroup(cadre_choice_material_2,"SD")
boutonexcel=Button(fenetre_choice_of_mat, text="TEST",width=15,height=1,bg="white",bd=5)
boutonexcel.pack()
fenetre_choice_of_mat.mainloop()
os.system("pause")
In this specific case, if you call pack on the button before any other widgets, and you set the side to "bottom", it will be centered at the bottom.
This is because the order in which pack is called matters. The packer works by allocating an entire side of the available space, and putting the widget on that side. The default side is "top", but setting it to "bottom" moves the widget to the bottom.
Once this button is on the bottom, that space is unavailable to any other widgets. Any other widgets that are subsequently packed must appear above it since that is where the unallocated space is.
Also, the default for the packer is to place the widget in the center of the space allocated for it. So, unless you specify otherwise the button will be in the middle of the bottom of the window.
boutonexcel=Button(fenetre_choice_of_mat, text="TEST",width=15,height=1,bg="white",bd=5)
boutonexcel.pack(side="bottom")
cadre_choice_material_1=Frame(fenetre_choice_of_mat,highlightbackground="blue", highlightthickness=2)
...
Ok I found by myself the solution.
Below is the code with the comments for the new lines. For a better readibility, I put "SOLUTION:" in the comment.
import os
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
fenetre_choice_of_mat=Tk()
fenetre_choice_of_mat.geometry("350x500")
class Checkbuttongroup:
def __init__(self, fenetre1, text1):
self.Checkbutton1 = IntVar()
self.texte=text1
self.fenetre12=fenetre1
Button1 = Checkbutton(self.fenetre12, text = self.texte,
variable = self.Checkbutton1,
onvalue = 1,
offvalue = 0,
height = 2,
width = 20,
anchor = "w")
Button1.pack()
bottom_frame=Frame(fenetre_choice_of_mat) #-- SOLUTION: I created a frame to put at the bottom
bottom_frame.pack(side='bottom')
cadre_choice_material_1=Frame(fenetre_choice_of_mat,highlightbackground="blue", highlightthickness=2)
cadre_choice_material_1.pack(side='left')
cadre_choice_material_2=Frame(fenetre_choice_of_mat,highlightbackground="blue", highlightthickness=2)
cadre_choice_material_2.pack(side='left')
grp_material_1=Checkbuttongroup(cadre_choice_material_1,"CW")
grp_material_2=Checkbuttongroup(cadre_choice_material_1,"Car Sling")
grp_material_4=Checkbuttongroup(cadre_choice_material_1,"Accessories electric")
grp_material_6=Checkbuttongroup(cadre_choice_material_1,"CW Filler")
grp_material_7=Checkbuttongroup(cadre_choice_material_1,"Shaft equipment")
grp_material_3=Checkbuttongroup(cadre_choice_material_1,"Rail for car and CW")
grp_material_5=Checkbuttongroup(cadre_choice_material_1,"Car doors")
grp_material_8=Checkbuttongroup(cadre_choice_material_2,"Floor selector")
grp_material_9=Checkbuttongroup(cadre_choice_material_2,"COP")
grp_material_11=Checkbuttongroup(cadre_choice_material_2,"Car")
grp_material_12=Checkbuttongroup(cadre_choice_material_2,"Adjustable bracket")
grp_material_13=Checkbuttongroup(cadre_choice_material_2,"Control")
grp_material_14=Checkbuttongroup(cadre_choice_material_2,"LOP")
grp_material_10=Checkbuttongroup(cadre_choice_material_2,"Shaft doors")
boutonexcel=Button(bottom_frame, text="TEST",width=15,height=1,bg="white",bd=5) #-- SOLUTION: I put the button in the bottom_frame
boutonexcel.pack(side='bottom')
fenetre_choice_of_mat.mainloop()
os.system("pause")

Tkinter entry widget - typed values gets copied to other entries

I am trying to create multiple Entry widgets on different pages of a notebook widget. However, when I type something in one Entry, it gets automatically copied to corresponding Entry on all pages. Please help. Here is my code.
import tkinter as tk
from tkinter import ttk
from tkinter import *
app = Tk()
nb = ttk.Notebook(app)
pages = []
canvas_left = []
canvas_right = []
entry_labels = []
entry_values = []
for i in range(3):
pages.append(ttk.Frame(nb))
canvas_left.append(tk.Canvas(pages[i], width=500, height=400, bd=0, highlightthickness=0))
canvas_left[i].pack(side=LEFT)
entry_values.append([])
for j in range(8):
entry_values[i].append(Entry(app, width=20, text="Window "+str(j+1), fg="white", bg="gray", font=("Helvetica", 12)))
canvas_left[i].create_window(125, 20 + j*35, anchor="w", window=entry_values[i][j])
nb.add(pages[i], text="Display "+(i+1).__str__())
nb.pack(side=TOP)
app.mainloop()
The text option of Entry widget is the same as textvariable option. So you use same variable name across the frames and they will be updated at the same time when one of them is updated.
Remove the text option from those Entry(...).

tkinter: simple scrollable text

Question
I'm trying to make a 'ttk Label' which a) is 20 pixels high by 300 pixels wide, b) is scrollable (in this case horizontally), and c) uses the simplest code possible within reason (except for the fact that the text and scrollbar are both within a frame*). I've found stackoverflow to be helpful in describing the processes I need to go through (put the label in a frame, put the frame in a canvas, put the scroll bar next to or underneath the canvas and 'bind' them together somehow), but despite looking at a fair few docs and stackoverflow questions, I can't figure out why my code isn't working properly. Please could someone a) update the code so that it satisfies the conditions above, and b) let me know if I've done anything unnecessary? Thanks
*the frame will be going in a project of mine, with text that is relevant
Current code
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
myframe_outer = ttk.Frame(root)
mycanvas = tk.Canvas(myframe_outer, height=20, width=300)
myframe_inner = ttk.Frame(mycanvas)
myscroll = ttk.Scrollbar(myframe_outer, orient='horizontal', command=mycanvas.xview)
mycanvas.configure(xscrollcommand=myscroll.set)
myframe_outer.grid()
mycanvas.grid(row=1, sticky='nesw')
myscroll.grid(row=2, sticky='ew')
mycanvas.create_window(0, 0, window=myframe_inner, anchor='nw')
ttk.Label(myframe_inner, text='test ' * 30).grid(sticky='w')
root.mainloop()
Edit:
Current result
Answer
Use a readonly 'entry' widget - it looks the same as a label, and doesn't need to be put in a canvas.
Code
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
mytext = tk.StringVar(value='test ' * 30)
myframe = ttk.Frame(root)
myentry = ttk.Entry(myframe, textvariable=mytext, state='readonly')
myscroll = ttk.Scrollbar(myframe, orient='horizontal', command=myentry.xview)
myentry.config(xscrollcommand=myscroll.set)
myframe.grid()
myentry.grid(row=1, sticky='ew')
myscroll.grid(row=2, sticky='ew')
root.mainloop()
Result

Categories