I have a code but After add just Label frame not working fine like that :
label = Label(self, text="Test-1")
label .grid(row=1, stick="w", padx=20)
The Label not go in Left.
but after add ttk :
label = ttk.Label(self, text="Test-1")
label .grid(row=1, stick="w", padx=20)
the Label Frame work fine why?
Because in the first one you didn't use this phrase
from tkinter import *
The * imports everything related to tkinter but
if you used this
from tkinter import ttk
you only import the function that create the table only
Related
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:
I wrote the following Tkinter code. When a user slides the scale widget, the font-size on the label is changed accordingly. I managed to do this but didn't understand why the function i wrote is returning a value when i didn't define it to do so. Is Tkinter returning something invisible implicitly ?
here is the code ...
from tkinter import *
from tkinter import ttk
master = Tk()
master.geometry('650x350+50+200')
scale_1 = ttk.Scale(master, length=300, from_=10, to=60)
scale_1.pack(padx=20, pady=30, anchor='nw')
label_1 = ttk.Label(master, text='Hello World !!!', background='orange')
label_1.pack(padx=10, pady=10, expand=True)
def changeFontSize():
label_1.config(font=('candara', int(scale_1.get())))
scale_1.config(command=changeFontSize) # problem area
master.mainloop()
following was my workaround
def changeFontSize(x)
or
scale_1.config(command= lambda x: changeFontSize())
I'm trying to make a text box having h scrollbar on its side.
How can I expand it top to bottom ?
(other widgets are expanded using sticky = N + S + E + W, but got an error:
TypeError: must be str, not Scrollbar
this is example code:
from tkinter import *
root = Tk()
text = Text(root)
text.grid(row=0, column=0)
S = Scrollbar(root)
S.grid(row=0, column=1)
S.config(command=text.yview)
text.config(yscrollcommand=S.set)
root.mainloop()
Your error is due to a naming conflict: you have named your scrollbar the same way as the S tkinter constant.
I advise you to do import tkinter as tk instead of from tkinter import *, this way the tkinter constant S will be tk.S, so there won't be confusion with the variables you define.
I want to place a label inside a frame in tkinter, but I can't figure out how to actually get it inside.
import tkinter
from tkinter import *
W=tkinter.Tk()
W.geometry("800x850+0+0")
W.configure(background="lightblue")
FRAME=Frame(W, width=100, height =50).place(x=700,y=0)
LABEL=Label(FRAME, text="test").pack()
When I run this, it doesn't place the Label inside the frame, but just places it normally on the window.
What am I doing wrong?
In the line
FRAME=Frame(W, width=100, height =50).place(x=700,y=0)
You think you are returning a tk frame, but you are not! You get the return value of the place method, which is None
So try
frame = Frame(W, width=100, height=50)
frame.place(x=700, y=0)
label = Label(frame, text="test").pack()
If you don't want the frame to shrink to fit the label, use (How to stop Tkinter Frame from shrinking to fit its contents?)
frame.pack_propagate(False)
Note: Either import tkinter or from tkinter import * but not both. Also, by convention, names of instances of objects are lowercase.
I think it's because you're assigning FRAME to Frame(W, width=100, height =50).place(x=700,y=0), as opposed to just the actual frame, and according to the Place Manager reference, there doesn't seem to be a return value. Try this:
import tkinter
from tkinter import *
W=tkinter.Tk()
W.geometry("800x850+0+0")
W.configure(background="lightblue")
FRAME=Frame(W, width=100, height =50)
FRAME.place(x=700,y=0)
LABEL=Label(FRAME, text="test").pack()
W.mainloop()
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