Is it possible to add an image to a combobox in tkinter?
I tried the following, but I already expected it to not work.
import tkinter as tk
from tkinter import ttk
from tkinter import *
# Creating tkinter window
window = tk.Tk()
window.title('Combobox')
window.geometry('1440x900')
main_canvas = Canvas(
window,
bg = "#FFFFFF",
height = 900,
width = 1440,
bd = 0,
highlightthickness = 0,
relief = "ridge"
)
main_canvas.place(x = 0, y = 0)
main_button_image_1 = PhotoImage(
file="button_1.png")
n = tk.StringVar()
monthchoosen = ttk.Combobox(window, width = 27, textvariable = n, image=main_button_image_1)
# Adding combobox drop down list
monthchoosen['values'] = ('new')
monthchoosen.place(
x=10.0,
y=826.0,
width=55.0,
height=55.0
)
monthchoosen.current(0)
window.mainloop()
Basically I want to make the combobox look a bit better and the easiest way would be to design an image and add it to the combobox.
PS:
I just copied real quick a few thinks, so please don't mind the variable names.
No, the ttk.Combobox widget does not support images.
Related
How to create large extry box in python tkinter?
I have tried to use height in ttk.entry() but the error show :
_tkinter.TclError: unknown option "-height"
from tkinter import *
from tkinter import ttk
GUI = Tk()
GUI.title("myTest")
GUI.geometry("700x700")
S_NOTE = StringVar()
E_NOTE = ttk.Entry(GUI, textvariable = S_NOTE, font = FONT1, width = 40, height = 20)
E_NOTE.grid(row = 0, column = 0, columnspan = 2, rowspan = 2)
GUI.mainloop()
I also need to get the StringVar from the entrybox and fix the position (such as using grid)
Looks like you are using a bad way to do this..
see.. You can use the Text widget to do the same..
Example:
from tkinter import *
GUI = Tk()
GUI.title("myTest")
GUI.geometry("700x700")
def set_text_to_variable():
global E_NOTE
global S_NOTE
S_NOTE = E_NOTE.get(1.0,END)
print("S_NOTE = ",S_NOTE)
E_NOTE = Text(GUI, font = "Segoe", width = 40, height = 20)
E_NOTE.grid(row = 0, column = 0, columnspan = 2, rowspan = 2)
Change_variable = Button(GUI, text = "CHANGE THE \"S_NOTE\" VARIABLE", command = set_text_to_variable)
Change_variable.grid()
GUI.mainloop()
I am trying to code a game for my school assignment. In this game, I wanted to have a mute button so I have made a button on top of a label frame and place it in a label. I don't know what is wrong with it but the image does not show up. I have tried to create a local copy by assigning it to a temp variable and yet it still doesn't show up. Here's my code:
from tkinter import ttk
from PIL import Image, ImageTk
root = Tk()
topFrame =Frame(root, width=500, height=50,)
topFrame.grid(row=0, column= 0)
btnframe = LabelFrame(topFrame, width = 20, height = 20)
btnframe.place(x = 450, y= 5 )
mute_image = Image.open("pygame/mute.png")
mute_image = mute_image.resize((50,50), Image.ANTIALIAS)
mute_icon = ImageTk.PhotoImage(mute_image)
mute_button = Button( btnframe, width = 50, height = 50, command = Mute, image = mute_icon)
mute_button.image = mute_icon
mute_button.pack()
root.mainloop()
Please go easy on me, this is my first time coding a game ever:)) Thanks in advance:))
Are you getting any error messages. I slightly modified your code and it complained abuot the button callback function was not defined. After I added that it all seems to work.
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
topFrame =Frame(root, width=500, height=50,)
topFrame.grid(row=0, column= 0)
btnframe = LabelFrame(topFrame, width = 20, height = 20)
btnframe.place(x = 450, y= 5 )
def Mute():
pass
mute_image = Image.open("images/beer.png")
mute_image = mute_image.resize((50,50), Image.ANTIALIAS)
mute_icon = ImageTk.PhotoImage(mute_image)
mute_button = Button(btnframe, width=50, height=50,
command=Mute, image=mute_icon)
mute_button.image = mute_icon
mute_button.pack()
root.mainloop()
I followed some tutorial on attaching a scrollbar to a textbox. However, in the tutorial, the scrollbar is really a "bar". When I tried myself, I can only press the arrows to move up or down, the middle part is not movable. May I know what I did wrong?
import tkinter as tk
root = tk.Tk()
scroll = tk.Scrollbar(root)
scroll.grid(row = 0, column = 1)
message = tk.Text(root, yscrollcommand = scroll.set, height = 25, width = 60)
message.grid(row = 0, column = 0)
for i in range(50):
message.insert(tk.END, f'This is line {i}\n')
scroll.config(command = message.yview)
root.mainloop()
You just have to add sticky='nsew' to Scrollbar widget.
sticky='nsew' will make the Scrollbar widget to expand to fill up the entire cell (at grid position row=0 & column=1) at every side (n-north, s-south, e-east, w-west)
Here is the code:
import tkinter as tk
root = tk.Tk()
scroll = tk.Scrollbar(root)
# add sticky option to the Scrollbar widget
scroll.grid(row = 0, column = 1, sticky='nsew')
message = tk.Text(root, yscrollcommand = scroll.set, height = 25, width = 60)
message.grid(row = 0, column = 0)
for i in range(50):
message.insert(tk.END, f'This is line {i}\n')
scroll.config(command = message.yview)
root.mainloop()
My program has a library which opens a new window
This is the library (its called make_entry):
from tkinter import *
def Create():
Window = Tk() # window
Window.geometry("900x500+50+50") # heightxwidth+x+y
mainPanel = Canvas(Window, width = 900, height = 500) # main screen
mainPanel.pack()
anyvar = StringVar() # the text in the entry
entry = Entry(mainPanel, width = 40, font = ("Purisa", 12, "bold"), justify = "center", textvariable = anyvar) # the entry
mainPanel.create_window(200, 100, window = entry)
anyvar.set("This doesnt work!!!!!")
Window.mainloop()
#Create()
If I run this library by itself then everything works fine, but when I import it from another program the only thing which doesn't work is anyvar.set("This doesnt work!!!!!").
Here is where I import it: (most of this code is cut out)
from tkinter import *
Window = Tk()
import make_entry
make_entry.Create()
Window.mainloop()
Is there a way to fix this problem without removing any of the windows?
You have two instances of Tk() which confuses Tkinter. I am guessing that the StringVar() is going to the other (first) instance. Instead, pass the only instance to the function, and use a Toplevel for a new window.
from tkinter import *
def Create(root):
window=Toplevel(root)
window.geometry("900x500+50+50") # heightxwidth+x+y
mainpanel = Canvas(window, width = 900, height = 500) # main screen
mainpanel.pack()
anyvar = StringVar() # the text in the entry
entry = Entry(mainpanel, width = 40, font = ("Purisa", 12, "bold"), justify = "center", textvariable = anyvar) # the entry
mainpanel.create_window(200, 100, window = entry)
anyvar.set("This doesnt work!!!!!")
and
from tkinter import *
Window = Tk()
import make_entry
make_entry.Create(Window)
Window.mainloop()
How can I put the button widget on top of the background image?
from Tkinter import *
import tkMessageBox
root = Tk()
# load image
logo = PhotoImage(file="C:\\Users\\admin\\Desktop\\project\\front page.gif")
w = logo.width()
h = logo.height()
root.geometry('%dx%d+0+0' % (w,h))
def helloCallBack():
tkMessageBox.showinfo("Hello Python", "Hello World")
#create widgets
B = Button(root,text = "ASSOCIATIONS", command = helloCallBack,width = 20,padx = 20)
B.pack()
B1 = Button(root,text = "HELP", command = helloCallBack,width = 20,padx = 20)
B1.place(relx=1, x=-50, y=2, anchor=NE)
B1.pack()
w1 = Label(root, compound = CENTER, image = logo).pack(side="right")
root.mainloop()
The easiest thing to do is to put a background image in a widget using place, then put other widgets in that widget the way you normally do with either pack or grid
background=Label(root, image=logo).place(x=0,y=0,relwidth=1, relheight=1)
b = Button(root, ...).pack(...)
Make sure you create the background first so that it has a lower stacking order.
If you really want to put a button in a label, just make the label the parent of the button and use pack or grid the way you normally do -- it's perfectly legal to pack something inside a Button or Label or any other widget (though, the end result might not be what you expect).