Related
I'm working on GUI project and I'm trying to configure a button color and text but it gives me an error..
here is a sample of my code:
from tkinter import*
from tkinter import ttk
#root
root = Tk()
root.geometry('640x520')
#Canvas
myCanvas = Canvas(root, width=350, height=300, bd=0, highlightthickness=0)
myCanvas.pack(fill='both', expand=True)
def qu1():
global myCanvas
myCanvas.itemconfig(Q1,bg='green',text= 'Done')
Q1 = Button(root, width=15, height=10, bg='#F3C4B7',fg='white', text='1', command=qu1)
myCanvas.create_window(10,10, anchor='nw', window=Q1)
root.mainloop()
it gives me this error:
line 12, in qu1
myCanvas.itemconfig(Q1,bg='green',text= 'Done')
_tkinter.TclError: invalid boolean operator in tag search expression
Already stated:
itemconfig applies to Canvas objects
You can do:
from tkinter import*
root = Tk()
root.geometry('640x520')
myCanvas = Canvas(root, width=350, height=300, bd=0, highlightthickness=0)
myCanvas.pack(fill='both', expand=True)
buttonBG = myCanvas.create_rectangle(0, 0, 100, 30, fill="grey40", outline="grey60")
buttonTXT = myCanvas.create_text(50, 15, text="click")
def qu1(event):
myCanvas.itemconfig(buttonBG, fill='red')
myCanvas.itemconfig(buttonTXT, fill='white')
myCanvas.tag_bind(buttonBG, "<Button-1>", qu1)
myCanvas.tag_bind(buttonTXT, "<Button-1>", qu1)
root.mainloop()
Or change the button itself:
from tkinter import*
root = Tk()
root.geometry('640x520')
myCanvas = Canvas(root, width=350, height=300, bd=0, highlightthickness=0)
myCanvas.pack(fill='both', expand=True)
def qu1():
Q1.configure(bg="#234")
Q1 = Button(root, width=15, height=10, bg='#F3C4B7',fg='white', text='1', command=qu1)
myCanvas.create_window(10,10, anchor='nw', window=Q1)
root.mainloop()
I have tried using a formatted code that already existed where I imported 'PIL' from 'ImageTk' however, that has not worked. The image and script is in the same file directory yet the image data is not recognizable. I changed the string to 'raw string' by adding the 'r' in front of the file path and I even tried using a different image and it still outputs the same error. This line linebackground_image = tk.PhotoImage(file=r'C:\Users\kevan\OneDrive\Desktop\Programming for me\Python\Tkinker\bgfortest.png') of the code is the source of error.
Your help would be appreciated
import tkinter as tk
HEIGHT = 700
WIDTH = 800
root = tk.Tk()
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
background_image = tk.PhotoImage(file=r'C:\Users\kevan\OneDrive\Desktop\Programming for me\Python\Tkinker\bgfortest.png')
background_label = tk.Label(root, image=background_image)
background_label.place(x=0, y=0, relheight=1, relwidth=1)
frame = tk.Frame(root, bg='#80c1ff', bd=5)
frame.place(relx=0.5, rely=0.1, relheight=0.1, relwidth=0.75, anchor='n')
button = tk.Button(frame, text="Hello", font=40)
button.place(relx=0.7, relheight=1, relwidth=0.3)
entry = tk.Entry(frame, bg='white', font=40)
entry.place(relheight=1, relwidth=0.65)
lower_frame = tk.Frame(root, bg='#80c1ff', bd=10)
lower_frame.place(relx=0.5, rely=0.25, relheight=0.6, relwidth=0.75, anchor='n')
label = tk.Label(lower_frame, text="This is a python GUI.", bg='cadet blue', fg="Black")
label.place(relwidth=1, relheight=1)
root.mainloop()
I am trying to update the label text in "label_enter_what" in accordance to what they chose in "drop". So if they choose "Energy", the label would change to: Enter wavelength in chosen unit below", for example. Sorry if the code looks messy, it's my first time coding. This is supposed to be a photon property calculator that i am making for fun because in physics we are currently doing this, but with pens and calculators.
import tkinter as tk
from tkinter import *
from PIL import ImageTk, Image
HEIGHT = 600
WIDTH = 900
root = tk.Tk()
root.title("Photon property calculator")
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
background_image = ImageTk.PhotoImage(Image.open('interference.jpg'))
background_label = tk.Label(root, image=background_image)
background_label.place(relheight=1, relwidth=1)
frame = tk.Frame(root, bg='#3E3E3E', bd=5)
frame.place(relx=0.5, rely=0.1, relheight=0.1, relwidth=0.75, anchor='n')
frame_upper = tk.Frame(root, bg='#3E3E3E', bd=5)
frame_upper.place(relx=0.5, rely=0.03, relheight=0.06, relwidth=0.75, anchor='n')
label_what_to_calc = tk.Label(frame_upper, bg='white', text='Enter what \n to calculate')
label_what_to_calc.place(relx=0, rely=0, relheight=1, relwidth=0.15)
label_enter_what = tk.Label(frame_upper, bg='white', text='Enter * in a chosen unit below.')
label_enter_what.place(relx=0.2, relheight=1, relwidth=0.2)
label_unit = tk.Label(frame_upper, bg='white', text='')
label_unit.place(relx=0.5, relheight=1, relwidth=0.1)
lower_frame = tk.Frame(root, bg='#60A8FF', bd=10)
lower_frame.place(relx=0.5, rely=0.3, relheight=0.5, relwidth=0.75, anchor='n')
entry_value = tk.Entry(frame, font=40, bg='white')
entry_value.place(relx=0.175, rely=0, relheight=1, relwidth=0.4)
OPTIONS = [
"Energy",
"Frequency",
"Wavelength"
]
clicked = StringVar()
clicked.set(OPTIONS[0])
drop = tk.OptionMenu(frame, clicked, *OPTIONS)
drop.place(relx=0, rely=0, relheight=1, relwidth=0.15)
OPTIONS_UNITS = ["μm",
"nm",
"pm",
"aJ",
"zJ",
]
clicked_1 = StringVar()
clicked_1.set(OPTIONS_UNITS[1])
drop_units = tk.OptionMenu(frame, clicked_1, *OPTIONS_UNITS)
drop_units.place(relx=0.6, rely=0, relheight=1, relwidth=0.09)
button = tk.Button(frame, text='Calculate!', font=40, bg="#F96612", fg='black')
button.place(relx=0.7, relheight=1, relwidth=0.3)
label = tk.Label(lower_frame, bg='white', text=clicked.get())
label.place(relheight=1, relwidth=1)
root.mainloop()
You can use
tk.OptionMenu( ... command=function)
to run function which will get selected value and it will change text in label
import tkinter as tk
# --- functions ---
def on_select(value):
label['text'] = value
# --- main ---
root = tk.Tk()
label = tk.Label(root, text='?')
label.pack()
OPTIONS = ["Energy", "Frequency", "Wavelength"]
value_var = tk.StringVar(value=OPTIONS[0])
op = tk.OptionMenu(root, value_var, *OPTIONS, command=on_select)
op.pack()
root.mainloop()
I need help. I need to bring out the image background to my new created frame in genkeymenu function. But the problem is, once the frame is created, the image background only seems to change in the first created frame. I tried to search for solutions but nothing works. Can I ask what's the problem?
import Tkinter as tk
from Tkinter import *
from PIL import Image, ImageTk
def genkeymenu():
generatemenu = tk.Toplevel(mainmenu)
bg1 = ImageTk.PhotoImage(file="key2.jpg")
background_label = Label(image=bg1)
background_label.place(x=0, y=0)
background_label.image = bg1
keynamelabel = Label(generatemenu, text="Enter your key name")
keynameEntry = Entry(generatemenu)
keynameButton = Button(generatemenu, text="Enter")
check1024= Checkbutton(generatemenu, text="1024 bit")
check2048= Checkbutton(generatemenu, text="2048 bit")
check4096= Checkbutton(generatemenu, text="4096 bit")
tk.background_label.grid(row=0)
keynamelabel.grid(row=0)
keynameEntry.grid(row=1)
keynameButton.grid(row=2)
check1024.grid(row=3, column=0)
check2048.grid(row=3, column=1)
check4096.grid(row=3, column=2)
generatemenu.title("Generate Key")
generatemenu.mainloop()
mainmenu = tk.Tk()
bg = ImageTk.PhotoImage(file="key.jpg")
background_label = Label(image=bg)
background_label.place(x=0, y=0)
genkeybutton = Button(mainmenu, text= "Generate Key Pair", fg="black", command=genkeymenu)
encryptbutton = Button(mainmenu, text= "Encrypt your message", fg="black")
decryptbutton = Button(mainmenu, text= "Decrypt your message", fg="black")
background_label.grid(row=0)
genkeybutton.grid(row=0, column=0, sticky = N, rowspan=2)
encryptbutton.grid(row=0, column=0)
decryptbutton.grid(row=0, column=0, sticky=S)
mainmenu.title("RSA ENCRYPTION")
mainmenu.mainloop()
You specify all the widget to appear in the toplevel window but didn't do that for the background. background_label = Label(generatemenu, image=bg1)
import Tkinter as tk
from Tkinter import *
PIL import Image, ImageTk
def genkeymenu():
generatemenu = tk.Toplevel(mainmenu)
bg1 = ImageTk.PhotoImage(file="key2.jpg")
background_label = Label(generatemenu, image=bg1)# specify the window you it to appear.
background_label.place(x=0, y=0)
background_label.image = bg1
keynamelabel = Label(generatemenu, text="Enter your key name")
keynameEntry = Entry(generatemenu)
keynameButton = Button(generatemenu, text="Enter")
check1024= Checkbutton(generatemenu, text="1024 bit")
check2048= Checkbutton(generatemenu, text="2048 bit")
check4096= Checkbutton(generatemenu, text="4096 bit")
tk.background_label.grid(row=0)
keynamelabel.grid(row=0)
keynameEntry.grid(row=1)
keynameButton.grid(row=2)
check1024.grid(row=3, column=0)
check2048.grid(row=3, column=1)
check4096.grid(row=3, column=2)
generatemenu.title("Generate Key")
generatemenu.mainloop()
mainmenu = tk.Tk()
bg = ImageTk.PhotoImage(file="key.jpg")
background_label = Label(image=bg)
background_label.place(x=0, y=0)
genkeybutton = Button(mainmenu, text= "Generate Key Pair", fg="black",
command=genkeymenu)
encryptbutton = Button(mainmenu, text= "Encrypt your message", fg="black")
decryptbutton = Button(mainmenu, text= "Decrypt your message", fg="black")
background_label.grid(row=0)
genkeybutton.grid(row=0, column=0, sticky = N, rowspan=2)
encryptbutton.grid(row=0, column=0)
decryptbutton.grid(row=0, column=0, sticky=S)
mainmenu.title("RSA ENCRYPTION")
mainmenu.mainloop()
I am really new to python and currently I am trying to design a form using tkinter. I am stuck trying to insert an scrollbar and a form in a notebook since I haven't found an answer to my question, and it is simple "How can I insert a scrollbar and a form in a notebook tkinter widget?"... As you can see is simple for you, but not for a newbie like me!
However, this is what I have done so far, fortunately it does show the scrollbar, but it crashes when I try insert the form into the notebook!
Note: My python version is Python 2.7.3 with EPD_free 7.3-2 (32-bit)
import Tkinter
from Tkinter import *
from ttk import *
import tkMessageBox
import ttk
import Tkinter as tk
root = Tk()
root.title("Model_A")
root.resizable(0,0)
# start of Notebook (multiple tabs)
notebook = ttk.Notebook(root)
notebook.pack(fill=BOTH, expand=True)
notebook.pressed_index = None
#Child Frames
ContainerOne = Frame(notebook)
ContainerOne.pack(fill=BOTH, expand=True)
ContainerTwo = Frame(notebook)
ContainerTwo.pack(fill=BOTH, expand=True)
ContainerThree = Frame(notebook)
ContainerThree.pack(fill=BOTH, expand=True)
ContainerFour = Tkinter.Frame(notebook)
ContainerFour.pack(fill=BOTH, expand=True)
#Create the pages
notebook.add(ContainerOne, text='Mode A')
notebook.add(ContainerTwo, text='Mode B')
notebook.add(ContainerThree, text='Mode C')
notebook.add(ContainerFour, text='Mode D')
canvas = Canvas(ContainerOne, width=200, height=400)
scroll = Scrollbar(ContainerOne, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
canvas = Canvas(ContainerTwo, width=200, height=400)
scroll = Scrollbar(ContainerTwo, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
canvas = Canvas(ContainerThree, width=200, height=400)
scroll = Scrollbar(ContainerThree, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
canvas = Canvas(ContainerFour, width=200, height=400)
scroll = Scrollbar(ContainerFour, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
frame = Frame(canvas, width=200, height=1000)
canvas.create_window(100, 500, window=frame)
frameOne = None
def defocus(event):
event.widget.master.focus_set()
if __name__ == '__main__':
ContainerOne= Tkinter.Label(notebook, text=" 1. Enter Main Details: ", font=("fixedsys", "16","bold italic"))
frameOne.grid(row=2, columnspan=7, sticky='W', \
padx=5, pady=5, ipadx=5, ipady=5)
#Component Selection
componentComb= ttk.Combobox(ContainerOne, width="19")
componentComb = Combobox(ContainerOne, state="readonly", values=("A", "B", "C"))
componentComb.grid(column=4, row=4, columnspan="5", sticky="nswe")
componentComb.set("Main Selection")
root.mainloop()
If you take a look at the options of the Notebook widget, you can see that neither yview nor yscrollcommand are present. Besides, Frame widgets aren't scrollable either.
What you can do is to create a Canvas widget with a Scrollbar inside your frameOne, and then add a Frame to the canvas with create_window.
root = Tk()
root.resizable(0,0)
notebook = ttk.Notebook(root)
notebook.pack(fill=BOTH, expand=True)
notebook.pressed_index = None
container = Frame(notebook)
container.pack(fill=BOTH, expand=True)
notebook.add(container, text='Mode A')
canvas = Canvas(container, width=200, height=400)
scroll = Scrollbar(container, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
frame = Frame(canvas, bg='white', width=200, height=1000)
canvas.create_window(100, 500, window=frame)
root.mainloop()
I have solved the problem I had with python and tkinter inserting form into a notebook as well as a scrollbar. I want to thank #A.Rodas for his kind help.
This is my code, hope you find it useful!
import Tkinter
from Tkinter import *
from ttk import *
import tkMessageBox
import math
import ttk
import Tkinter as tk
def defocus(event):
event.widget.master.focus_set()
# start of GUI code
root = Tk()
root.title("Model A")
root.resizable(0,0)
# start of Notebook (multiple tabs)
notebook = ttk.Notebook(root)
notebook.pack(fill=BOTH, expand=True)
notebook.pressed_index = None
# Child frames
ContainerOne = Frame(notebook)
ContainerOne.pack(fill=BOTH, expand=True)
ContainerTwo = Frame(notebook)
ContainerTwo.pack(fill=BOTH, expand=True)
# Create the pages
notebook.add(ContainerOne, text='Mode A')
notebook.add(ContainerTwo, text='Mode B')
canvas1 = Canvas(ContainerOne, width=1200, height=450)
scroll = Scrollbar(ContainerOne, command=canvas1.yview)
canvas1.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas1.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
canvas2 = Canvas(ContainerTwo, width=1200, height=450)
scroll = Scrollbar(ContainerTwo, command=canvas2.yview)
canvas2.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas2.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
frameOne = Frame(canvas1, width=800, height=450)
canvas1.create_window(250, 125, window=frameOne)
frameTwo = Frame(canvas2, width=800, height=450)
canvas2.create_window(200, 140, window=frameTwo)
# Main Frame
#Close Application Button
def quit(root):
root.destroy()
ttk.Button(root, text="Close Application", command=lambda root=root:quit(root)).pack()
if __name__ == '__main__':
#Main Part
stepOne = Tkinter.LabelFrame(frameOne, text=" 1. Enter Main Details: ", font=("fixedsys", "16","bold italic"))
stepOne.grid(row=0, columnspan=5, sticky='nsew', padx=5, pady=5, ipadx=5, ipady=5)
stepTwo = Tkinter.LabelFrame(frameOne, text=" 2. Calculate AP : ", font=("fixedsys", "16","bold italic"))
stepTwo.grid(row=2, columnspan=7, sticky='WE', padx=5, pady=5, ipadx=5, ipady=5)
# First Step Starts
# Component Selection
componentComb= ttk.Combobox(stepOne, width="19")
componentComb = Combobox(stepOne, state="readonly", values=("CDA", "VHS", "DVD"))
componentComb.grid(column=4, row=0, columnspan="5", sticky="nswe")
componentComb.set("Component Selection")
# Temperature Selection
tempComb = ttk.Combobox(stepOne, width="12")
tempComb = Combobox(stepOne, state="readonly", values=("-40", "-30", "-20","-10", "0",))
tempComb.grid(column=0, row=2, columnspan="2", sticky="w")
tempComb.set("Temperature Selection")
# Second Step Starts
inEncLbl = Tkinter.Label(stepTwo, text="Oxide:")
inEncLbl.grid(row=2, column=0, sticky='E', padx=5, pady=2)
inEncTxt = Tkinter.Entry(stepTwo, width=6)
inEncTxt.grid(row=2, column=1, sticky='w', pady=2)
outEncLbl = Tkinter.Label(stepTwo, text="Density Rate (DR):")
outEncLbl.grid(row=2, column=5, padx=5, pady=2)
outEncTxt = Tkinter.Entry(stepTwo, width=6)
outEncTxt.grid(row=2, column=7,sticky='w', pady=2)
#End Code
root.mainloop()