Tkinker cursor that impacts a graph - python-3.x

I am not good at all at informatics.
I don't find anything on the website that explains how to change a value on a cursor that will impact a specific variable in a model that is represented in a graph. With X depending on the time (that is a loop).
We have the following easy model : X= L + Z + H and the 3 variable that are cursors.
Here is our code for a first cursor with tkinter:
from tkinter import *
def sel():
selection = "Value = " + str(var.get())
label.config(text = selection)
print(var.get())
root = Tk()
var = DoubleVar()
root = Tk()
var = DoubleVar()
scale = Scale( root, length = 150, from_=-3, to=5, variable = var)
scale.pack(anchor = CENTER)
button = Button(root, text = "Get Scale Value", command = sel)
button.pack(anchor = CENTER)
label = Label(root)
label.pack()
root.mainloop()
Thank you very much !

Related

Im having troubles with python(tkinter) gui with the layout

# Import module
from tkinter import *
import pyautogui, time
# Create object
root = Tk()
# Adjust size
root.geometry("350x350")
root.title("SycoBak's SpamBot")
# Add image file
bg = PhotoImage(file = "Slogo.png")
# Create Canvas
canvas1 = Canvas( root, width = 350,
height = 350)
canvas1.pack(fill = "both", expand = True)
# Display image
canvas1.create_image( 0, 0, image = bg,
anchor = "nw")
canvas1.create_text(250,250, text="SycoSpamBot")
L1 = Label(canvas1, text="Word")
L1.pack( side = LEFT)
E1 = Entry(canvas1)
E1.pack(side = RIGHT)
canvas1.create_window(100, 100, window=L1)
L2 = Label(canvas1, text="Amount")
L2.pack( side = LEFT)
E2 = Entry(canvas1)
E2.pack(side = RIGHT)
canvas1.create_window(100, 150, window=L2)
def Spammer ():
time.sleep(5)
x = 0
amount = int(E2.get())
word = E1.get()
while (amount > x):
pyautogui.typewrite(word)
pyautogui.press("enter")
x=x+1
button1 = Button(root, text = "Start", command=Spammer, bg="green")
button1_canvas = canvas1.create_window( 100, 10,
anchor = "nw",
window = button1)
root.mainloop()
Im wondering if I could make it so that the start button is at the bottom, and the word and amount labels are on the left side of a text input box.
This is how my GUI layout currently looks like. (Very Messy)
[1]: https://i.stack.imgur.com/UYvtU.png

Is there a possibility to assign a value to a tkinter button?

I want to make a battle ship game, where each player puts their different boats on an 10 x 10 field. To know where the boats where placed, I want to change a variable from False to True, if the button was clicked. Also the colour of the button should change to black, but if it is already black it shall change back to grey and also the variable should be false again.
In my code I defined a class only for the buttons. master should be a frame. The command 'put command' is needed, as I need these buttons for a second frame with another command. bentuzung means whoever is using it (in my case its either 1 or 2).
So I am searching for a way to assign a variable to a button. Also the code should not become too long.
from tkinter import *
class buttons():
def __init__(self, master, benutzung):
self.button_list = []
for j in range(10):
for m in range(10):
btn = Button(master, text = " ")
btn.grid(row = j, column = m)
self.button_list.append(btn)
self.put_command(benutzung)
def put_command(self, benutzung):
if benutzung == 1:
for x in self.button_list:
x.configure(command = lambda bt = x: self.set_ship(bt))
def set_ship(self, btn):
btn.configure(bg = "black")
So I found out a solution myself. You can just create a new attribute e.g. 'num' and assign a value to it. Then you can get the value by 'btn.num'.
Code:
from tkinter import *
class buttons():
def __init__(self, master, benutzung):
self.button_list = []
for j in range(10):
for m in range(10):
self.btn = Button(master, text = " ", bg = "grey")
self.btn.grid(row = j, column = m)
self.btn.num = 1
self.button_list.append(self.btn)
self.put_command(benutzung)
def put_command(self, benutzung):
if benutzung == 1:
for x in self.button_list:
x.configure(command = lambda bt = x: self.set_ship(bt))
def set_ship(self, btn):
if btn.num == 1:
btn.configure(bg = "black")
btn.num = 0
elif btn.num == 0:
btn.configure(bg = "grey")
btn.num = 1
You can use the textvariable parameter with IntVar().
So your button would be:
num = IntVar()
b = Button(master, text = "str", textvariable = num)
b.pack()
Hopefully this helps!

how to use multiple frames in a canvas with scrollbar in tkinter?

I am building a GUI app for showing some data results.I want to show name and some details in a frame for each result I have.So I created a frame and a canvas inside of it.Now I want to place multiple frames in canvas with scrollbars but it didn't seems to work .So I tried to place results in multiple canvas but its is also not working with scrollbar.Here is a snipet of it.
from tkinter import *
class Results:
def __init__(self,master,topic = None):
self.result_frame = Frame(master, bg = '#ffffff', bd =5)
self.result_frame.place(relx=0.5,rely=0.25, relwidth = 1,relheight =
0.75, anchor ='n')
self.canvas = Canvas(self.result_frame,bg =
'#ffffff',bd=0,width=500,height=500)
self.canvas.place(relx = 0.0, rely = 0.0 )
self.sbar = Scrollbar(self.result_frame, orient = 'vertical')
self.sbar.config(command = self.canvas.yview)
self.sbar.place(relx = 0.98,width = 14 ,relheight = 0.5 )
self.canvas.configure(yscrollcommand=self.sbar.set)
self.rframe = Frame(self.canvas)
self.rframe.pack()
self.canvas.create_window((0,0), window=self.rframe, anchor="nw")
self.show_results()
def create_rcanvas(self,name,y) :
name = Canvas(self.rframe, bg = '#4da6ff')
name.place(relx=0.25,rely = y, relwidth = 0.55,height = 100)
def show_results(self):
y = 0.1
for i in range(0,10):
name = 'can'+str(i)
name = Canvas(self.canvas, bg = '#4da6ff')
name.place(relx=0.25,rely = y, relwidth = 0.55,height = 100)
y = y + 0.2
if __name__ == '__main__':
root = Tk()
root.geometry('600x600')
Results(root)
root.mainloop()
Kindly provide me solution and please try to run and check it first.

How do I find out if I have pressed the checkbutton?

from Tkinter import *
window = Tk()
window.config(background="green")
window.bind("<Escape>", quit)
cbttn = Checkbutton(text="Caps?").grid(row=3, column=0)
Does if cbttn = True work for it? Or do I have to move the .grid() function and move it to the next line of code.
An assignment such as:
cbttn = Checkbutton(text="Caps?").grid(row=3, column=0)
yields in cbttn being of an object of None type.
Either remove the assignment to cbttn (if you do not want to reference it further in the script)
Checkbutton(text="Caps?").grid(row=3, column=0)
or move grid to a new line as:
cbttn = Checkbutton(text="Caps?")
cbttn.grid(row=3, column=0)
And to see if the Checkbutton was pressed or not, use the command option available. Check the example here. Taking this example:
from tkinter import *
def display():
print(CheckVar1.get())
top = Tk()
CheckVar1 = IntVar()
CheckVar2 = IntVar()
C1 = Checkbutton(top, text = "Music", variable = CheckVar1, \
onvalue = 1, offvalue = 0, height=5, \
width = 20, command = display)
C2 = Checkbutton(top, text = "Video", variable = CheckVar2, \
onvalue = 1, offvalue = 0, height=5, \
width = 20)
C1.pack()
C2.pack()
top.mainloop()
The output should keep switching between 0 and 1

Finding coordinates on a scrollable Tkinter canvas

I'm trying to make it so that when you right click, the row is highlighted blue and you can then edit or delete that entry. However, since adding a scrollbar, if the page is scrolled then the selection will be offset.
I have tried to suggested thing of finding the canvasx and canvasy and then using find_closest(0, however when i do that it always returns (1,) no matter what.
Canvasx and canvasy seem local to each label, not the canvas itself
from tkinter import *
def RightClick(event):
#Find widgets on the row that was right clicked
print(canvas.canvasy(event.y))
for widget in frame.winfo_children():
mouseClickY = event.y_root - root.winfo_y() - widget.winfo_height()
widgetTopY = widget.winfo_y()
widgetBottomY = widget.winfo_y() + widget.winfo_height()
if (widget.winfo_class() == "Label") and (mouseClickY > widgetTopY) and (mouseClickY < widgetBottomY):
#Highlight that row
if widget.cget("bg") != "#338fff":
widget.config(bg = "#338fff", fg="#FFFFFF")
#Deselect all rows
elif widget.winfo_class() == "Label":
widget.config(bg = "#FFFFFF", fg="#000000")
def onFrameConfigure(event):
canvas.configure(scrollregion=canvas.bbox("all"))
root = Tk()
root.bind("<Button-3>", RightClick)
canvas = Canvas(root, width = 1080, height=500)
frame = Frame(canvas)
scrollbar = Scrollbar(root, command=canvas.yview)
canvas.config(yscrollcommand=scrollbar.set)
canvas.grid(row=1,column=0,columnspan=5)
canvas.create_window((0,0), window=frame, anchor="nw",tags="frame")
scrollbar.grid(column=5,row=1,sticky="NS")
frame.bind("<Configure>", onFrameConfigure)
for countY in range(40):
for countX in range(6):
l = Label(frame, text=countX, width = 25, height = 1, bg="#FFFFFF")
l.grid(column=countX,row=countY+1)
Solved it, turns out
mouseClickY = event.y_root - root.winfo_y() - widget.winfo_height()
widgetTopY = widget.winfo_y()
widgetBottomY = widget.winfo_y() + widget.winfo_height()
should be
mouseClickY = event.y_root - root.winfo_y()
widgetTopY = widget.winfo_rooty()
widgetBottomY = widget.winfo_rooty() + widget.winfo_height()
OR
I should have used winfo_containing, which is a lot neater

Resources