window = tkinter.Tk()
window.geometry('700x700')
text_eingabe = Entry(window)
text_eingabe.place(x = 250, y = 550)
def coords():
xy = int(text_eingabe.get())
C.create_line(10,10,{xy},200, fill= 'blue')
eingabe_bestätigung = Button(window, text= 'bitte bestätigen', command= coords)
eingabe_bestätigung.place(x = 250, y= 500)
C = Canvas(window, bg = 'white',width= 300, height= 300)
C.place(x= 170, y= 100)
window.mainloop()
The Problem is that i am getting a error called 'bad screen distance'
when im trying to put numbers into the entry box to 'convert' these to the variables of the line
{xy} creates a set. You just want to use the variable so the correct code is:
window = tkinter.Tk()
window.geometry('700x700')
text_eingabe = Entry(window)
text_eingabe.place(x = 250, y = 550)
def coords():
xy = int(text_eingabe.get())
C.create_line(10,10,xy,200, fill= 'blue')
eingabe_bestaetigung = Button(window, text= 'bitte bestätigen', command= coords)
eingabe_bestaetigung.place(x = 250, y= 500)
C = Canvas(window, bg = 'white',width= 300, height= 300)
C.place(x= 170, y= 100)
window.mainloop()
Related
This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 6 months ago.
I'm trying to display a frame with a canvas inside in order I'll be able to change of frame with a button, but when the code is executed it does not even display the button, I don't know if it happens because of all is on a function, if doing what I think is even possible or if I'm missing something
from tkinter import *
window = Tk()
window.geometry("1200x700")
window.configure(bg = "#ffffff")
def btn_clicked():
print("Button Clicked")
frame1 =Frame(window, width=1200, height=700)
frame1.grid(row=0, column=0)
def load_page():
frame1.tkraise()
frame1.pack_propagate(False)
canvas = Canvas(
frame1,
bg = "#263ff8",
height = 700,
width = 1200,
bd = 0,
highlightthickness = 0,
relief = "ridge")
canvas.place(x = 0, y = 0)
background_img = PhotoImage(file = f"background.png")
background = canvas.create_image(
601.0, 341.0,
image=background_img)
img0 = PhotoImage(file = f"img0.png")
boton = Button(
image = img0,
borderwidth = 0,
highlightthickness = 0,
command = btn_clicked,
relief = "flat")
boton.place(
x = 85, y = 71,
width = 430,
height = 99)
load_page()
window.resizable(False, False)
window.mainloop()
Rephrased the entire code and including image:
To see button display. I had to reduced height and width in Canvas. Also The button had to reduced. In line 21, I changed bd = 10. You can comment in , because I don't used PhotoImage. you can debug see in image.
from tkinter import *
window = Tk()
window.geometry("1200x700")
window.configure(bg = "#ffffff")
def btn_clicked():
print("Button Clicked")
frame1 =Frame(window, width=1200, height=700)
frame1.grid(row=0, column=0)
def load_page():
frame1.tkraise()
frame1.pack_propagate(False)
canvas = Canvas(
frame1,
bg = "#263ff8",
height = 100,
width = 200,
bd = 10,
highlightthickness = 0,
relief = "ridge")
canvas.place(x = 0, y = 0)
#background_img = PhotoImage(file = f"background.png")
#background = canvas.create_image(
#601.0, 341.0,
#image=background_img)
#img0 = PhotoImage(file = f"img0.png")
boton = Button(frame1,
#image = img0,
borderwidth = 0,
highlightthickness = 0,
command = btn_clicked,
relief = "flat")
boton.place(
x = 85, y = 71,
width = 30,
height =29)
load_page()
window.resizable(False, False)
window.mainloop()
Output result. you can see debug.
trying to have the same label entry as the first frame to the second frame. have tried to add the second label to my function, however, it isn't registering as a label at all. trying to figure how the label entry can be saved. not sure if I should save it as a variable and then display it or just display the same label in both frames.
import tkinter as tk
import tkinter.messagebox as box
import csv
from tkinter import *
window = tk.Tk()
window.state('zoomed')
window.title('freshta otdering system')
def show_frame(fram):
fram.tkraise()
frame2 = tk.Frame(window)
frame3 = tk.Frame(window)
for frame in (frame1, frame2, frame3):
frame.grid(row=0,column=0,sticky='nsew')
filepath = '/Users/adamcleaver/Desktop/ICT/SAT part 1 /orders.csv'
File = open(filepath)
reader = csv.reader(File)
Data = list (reader)
del(Data[0])
list_of_entries = []
for x in list(range(0,len(Data))):
list_of_entries.append(Data[x][0])
var = StringVar(value = list_of_entries)
listbox1 = Listbox(frame2, listvariable = var)
listbox1.place(x= 10, y = 220, height = 500, width = 200)
def update():
index = listbox1.curselection()[0]
Foodlabel3.config(text = Data[index][1]) and Foodlabel2.config(text = Data[index][1])
Drinkslabel3.config(text = Data[index][2]) and Drinkslabel2.config(text = Data[index][2])
Pricelabel3.config(text = Data[index][3]) and Pricelabel2.config(text = Data[index][3])
return None
#using update function within the button
button1 = tk.Button(frame2, text="Update", command=update, fg = "blue", bg='yellow')
button1.place(x=400, y=450, height = 75, width = 125)
button2 = tk.Button(frame2, text="continue", command= lambda:show_frame(frame3), fg = "blue", bg='yellow')
button2.place(x=500, y=450, height = 75, width = 125)
Foodlabel = Label(frame2, text="Food", font=('Arial',20,'bold'),bg = '#F0EAD6').place(x= 295, y= 200)
Drinkslabel = Label(frame2, text="Drinks",font=('Arial',20,'bold'),bg = '#F0EAD6').place(x= 295, y= 300)
Pricelabel = Label(frame2, text="Total price ($)",font=('Arial',20,'bold'),bg = '#F0EAD6').place(x= 275, y=400)
Foodlabel2 = Label(frame2, text="",font=('Arial',20),bg = '#F0EAD6')
Foodlabel2.place(x= 425 , y= 200)
Drinkslabel2 = Label(frame2, text="",font=('Arial',20),bg = '#F0EAD6')
Drinkslabel2.place(x= 425, y= 300)
Pricelabel2 = Label(frame2, text="",font=('Arial',20),bg = '#F0EAD6' )
Pricelabel2.place(x=425, y= 400)
#label for login
tk.Label(frame2, text = 'Ordering' ,font=('Arial',36), fg = 'red', bg = '#F0EAD6' ).place(x=0, y=125)
tk.Label(frame2, text = 'Your order :' ,font=('Arial',36), bg = '#F0EAD6' ).place(x= 320, y=125)
#==================Frame 3 code ========================================
#label for login
tk.Label(frame2, text = 'Ordering' ,font=('Arial',36), fg = 'red', bg = '#F0EAD6' ).place(x=0, y=125)
Foodlabel3 = Label(frame3, text="",font=('Arial',20),bg = '#F0EAD6')
Foodlabel3.place(x= 425 , y= 200)
Drinkslabel3 = Label(frame3, text="",font=('Arial',20),bg = '#F0EAD6')
Drinkslabel3.place(x= 425, y= 300)
Pricelabel3 = Label(frame3, text="",font=('Arial',20),bg = '#F0EAD6' )
Pricelabel3.place(x=425, y= 400)
frame3_btn = tk.Button(frame3, text='Enter',command=lambda:show_frame(frame1))
frame3_btn.pack(fill='x',ipady=15)
show_frame(frame1)
window.mainloop()
That is not how you use and. You need:
def update():
index = listbox1.curselection()[0]
Foodlabel3.config(text = Data[index][1])
Foodlabel2.config(text = Data[index][1])
Drinkslabel3.config(text = Data[index][2])
Drinkslabel2.config(text = Data[index][2])
Pricelabel3.config(text = Data[index][3])
Pricelabel2.config(text = Data[index][3])
I have written a code for a basic game but the image and shape don't show up unless I add something like item.pack() or win.mainloop() [which doesn't really make sense] but then the lines below it don't run.
When I don't have anything, the buttons show up but the image doesn't show up.
import tkinter as tk
import random
from tkinter import messagebox
win = tk.Tk()
my_label = tk.Label(win, text="Color of the Baloon Game")
my_label.pack()
my_canvas = tk.Canvas(win, width=400, height=600)
my_canvas.pack()
background_image=tk.PhotoImage(file = "CS_Game_menu.png")
background_label = tk.Label(my_canvas, image=background_image)
background_label.photo = background_image
background_label.grid(row = 0, rowspan = 10, column = 0, columnspan = 10)
def drawCircle():
color = "green"
x1 = 265
y1 = 80
diameter = 90
my_canvas.destroy()
circle_button.destroy()
quit_button.destroy()
my_label.destroy()
my_label1 = tk.Label(win, text="What is the Color of the Baloon?", font="Purisa")
my_label1.pack()
my_canvas1 = tk.Canvas(win, width=400, height=600)
my_canvas1.pack()
image1 = r"CS_Game_baloon.png"
photo1 = tk.PhotoImage(file=image1)
item = my_canvas1.create_image(200, 350, image=photo1)
shape = my_canvas1.create_oval(x1, y1, x1 + diameter, y1 + diameter+20, fill=color)
item.pack()
game1_button = tk.Button(my_canvas1, text = "Green")
game1_button.grid(row= 8, column = 3)
game1_button["command"] = lambda: messagebox.showinfo("Congratulations!", "Correct Answer!")
game2_button = tk.Button(my_canvas1, text = "Blue")
game2_button.grid(row= 8, column = 5)
game2_button["command"] = lambda: messagebox.showinfo("Sorry!", "Incorrect Answer!")
game3_button = tk.Button(my_canvas1, text = "Red")
game3_button.grid(row= 8, column = 7)
game3_button["command"] = lambda: messagebox.showinfo("Sorry", "Incorrect Answer!")
circle_button = tk.Button(win, text="New Game")
circle_button.pack()
circle_button["command"] = drawCircle
quit_button = tk.Button(win, text="Quit")
quit_button.pack()
quit_button['command'] = win.destroy
You are using both the create_... methods and grid methods on your canvas object. It won't behave as you expected.
To achieve what you want, you can create a Frame, put your buttons in it, and then use create_window method on your canvas:
def drawCircle():
...
shape = my_canvas1.create_oval(x1, y1, x1 + diameter, y1 + diameter+20, fill=color)
frame = tk.Frame(my_canvas1)
game1_button = tk.Button(frame, text = "Green")
game1_button.grid(row= 8, column = 3)
game1_button["command"] = lambda: messagebox.showinfo("Congratulations!", "Correct Answer!")
game2_button = tk.Button(frame, text = "Blue")
game2_button.grid(row= 8, column = 5)
game2_button["command"] = lambda: messagebox.showinfo("Sorry!", "Incorrect Answer!")
game3_button = tk.Button(frame, text = "Red")
game3_button.grid(row= 8, column = 7)
game3_button["command"] = lambda: messagebox.showinfo("Sorry", "Incorrect Answer!")
my_canvas1.create_window(200,500,window=frame)
And of course, add win.mainloop() to the bottom of your program if you haven't already.
I would like to continuously switch between two images after some interval lets say(200 ms) to get a animated flashing/blinking like appearance in the background. I have only managed to get still image for now.
This following code is the child window of a main program which is calling the function Refresher.
Main function:
def rad_alarm(self):
self.alarm = Tk.Toplevel()
self.alarm.geometry('1024x600')
#self.alarm.title("Alert")
vFont = tkFont.Font(family = 'Helvetica', size = 30)
#self.center(self.alarm)
self.alarm.overrideredirect(1)
#self.alarm.wm_attributes('-type', 'splash');
radfilename = PhotoImage(file = "radio1.png")
self.alarm.background_label = Label(self.alarm, image=radfilename)
self.alarm.background_label.pack()
bcklbl = Label(self.alarm, text = str(bck), bg="black", fg="Yellow", font=vFont)
bcklbl.place(x = 825, y = 300, width=140, height=40)
cpslbl = Label(self.alarm, text = str(counti), bg="black", fg="Yellow", font=vFont)
cpslbl.place(x = 825, y = 445, width=140, height=40)
dtlbl = Label(self.alarm, text = datetime.now().strftime('%Y-%m-%d %H:%M:%S'), bg="black", fg="Yellow", font=vFont)
dtlbl.place(x = 630, y = 140, width=380, height=40)
button=Button(self.alarm, text="Acknowledge Alarm", command=self.alarm_close, relief=FLAT, bg="black", fg="Red", font=vFont)
button.place(x = 400, y = 510, width=380, height=80)
dt=str(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
self.conn.execute("INSERT INTO ALARMS (CPS,BCK,DT) VALUES (?,?,?)",(counti, bck, dt));
self.conn.commit()
self.Refresher()
Function called for animation:
def Refresher(self):
sleep(0.1)
radfilename = PhotoImage(file = "radio2.png")
self.alarm.background_label.configure(image=radfilename)
sleep(0.1)
radfilename = PhotoImage(file = "radio1.png")
self.alarm.background_label.configure(image=radfilename)
self.alarm.background_label.after(200, self.Refresher)
As I said I am only getting a still image on background instead of flashing/blinking animation. Any idea where I am going wrong ?
I'm trying to understand how bind and events work in python. For example I've created 3 tiles and would like to be able to change the color of one of the tiles but cannot understand or figure out where I'm going wrong. I keep getting:
AttributeError: 'int' object has no attribute 'bind'.
Below is the code and thanks in advance:
import tkinter
def main():
root = tkinter.Tk()
title = tkinter.Label(root, text="Test Window")
title.pack()
canvas= tkinter.Canvas(root, background='green', width = 300, height = 300)
tile1=canvas.create_rectangle(0, 0, 100, 100, fill = 'magenta')
tile2=canvas.create_rectangle(100,0, 200,100, fill = 'blue')
tile3=canvas.create_rectangle(200,0, 300,100, fill = 'blue')
canvas.pack()
def change_square(event):
event.configure(background = 'blue')
tile1.bind("<Button-1>", change_square(tile1))
root.mainloop()
if __name__ == '__main__':
main()
itemconfigure will change the colour:
def main():
root = tkinter.Tk()
title = tkinter.Label(root, text="Test Window")
title.pack()
canvas = tkinter.Canvas(root, background='green', width=300, height=300)
s1 = canvas.create_rectangle(0, 0, 100, 100, fill='magenta')
s2 = canvas.create_rectangle(100, 0, 200, 100, fill='blue')
s3 = canvas.create_rectangle(200, 0, 300, 100, fill='blue')
canvas.pack()
def change_square(event):
canvas.itemconfigure(s1, fill="blue")
canvas.bind("<Button-1>", change_square)
root.mainloop()
If you want to change the middle to black you would use:
canvas.itemconfigure(s2, fill="black")`
And so on.
If you want to change colour based on which you click this should work:
def change_square(event):
x = canvas.canvasx(event.x)
y = canvas.canvasy(event.y)
sq = canvas.find_closest(x,y)[0]
canvas.itemconfigure(sq, fill="black")
canvas.bind("<Button-1>", change_square)
root.mainloop()