uing button clicks in a if stament tkinter - python-3.x

hi i am making a code using tkinter that has button and i want it to do different thing is the button is pressed.
import tkinter as tk
Height = 500
Width = 700
def comp():
if ? == ?:
print("hi")
elif ? == ?:
print("bye")
elif ? == ?:
print("trump")
elif ? == ?:
print("bidden")
window = tk.Tk()
canvas = tk.Canvas(window, height=Height, width=Width)
canvas.pack()
frame = tk.Frame(window)
frame.place(relx=0,rely=0, relheight=1, relwidth=1)
label = tk.Label(frame, text="hello malcom what would you like to do?")
label.place(relx=0.3, rely=0, relwidth=0.45, relheight=0.20)
button = tk.Button(frame, text="add someone new", bg='black', fg='white', command=comp)
button.place(relx=0.3, rely=0.2, relwidth=0.45, relheight=0.10)
button1 = tk.Button(frame, text="register a visit", bg='black', fg='white', command=comp)
button1.place(relx=0.3, rely=0.4, relwidth=0.45, relheight=0.10)
button2 = tk.Button(frame, text="view data", bg='black', fg='white', command=comp)
button2.place(relx=0.3, rely=0.6, relwidth=0.45, relheight=0.10)
button3 = tk.Button(frame, text="end program", bg='black', fg='white', command=comp)
button3.place(relx=0.3, rely=0.8, relwidth=0.45, relheight=0.10)
window.mainloop()
as you can see i have the buttons on a frame and i need it to do specific things dependat on what was pressed
please can someone advise on what to do?

Related

How to place buttons in tkinter side by side and fill Y at the top of the widget

I am trying to align three button at the top of my widget side by side and fill "Y" and "X" without changing the placement of the text widget which is just under them. I tried to place the button side by side using those anchor=NW,N,NE but it's not working i don't how to tell it in English, so here is an image of what I got:
Here is my code:
root = Tk()
screen_x = int(root.winfo_screenwidth())
screen_y = int(root.winfo_screenheight()) - int(root.winfo_screenheight()) * int(9.1145833333) // 100
window_x = 512
window_y = 690
posX = (screen_x // 2) - (window_x // 2)
posY = (screen_y // 2) - (window_y // 2)
geo = "{}x{}+{}+{}". format(window_x, window_y, posX, posY)
root.geometry(geo)
root.resizable(0,0)
root.title("StoryApp")
root.config(background="gray8")
photo2 = PhotoImage(file = "Newico.png")
root.iconphoto(False, photo2)
Btn1 = Button(root, text="Button1", command=passit, padx=5, pady=5,borderwidth=5, relief="sunken", activebackground="dark red", bg="gray15", fg="red")
Btn1.configure(height=1, width=6)
Btn1.pack(padx=10, pady=5, side=TOP, anchor=NW, fill=Y)
Btn2 = Button(root, text="Button2", command=passit, padx=2, pady=5,borderwidth=5, relief="sunken", activebackground="dark red", bg="gray15", fg="red")
Btn2.configure(height=1, width=6)
Btn2.pack(padx=10, pady=5,side=TOP, anchor=N, fill=Y)
Btn3 = Button(root, text="Button3", command=passit, padx=2, pady=5,borderwidth=5, relief="sunken", activebackground="dark red", bg="gray15", fg="red")
Btn3.configure(height=1, width=6)
Btn3.pack(padx=10, pady=5,side=TOP, anchor=NE, fill=Y)
DecodedTextBoxTitle = LabelFrame(root, text="Decoded code", padx=5, pady=5, height=260)
DecodedTextBoxTitle.config(background="gray8")
DecodedTextBoxTitle.config(foreground="red")
DecodedTextBoxTitle.pack(padx=10, pady=5, fill="both", expand=True)
DecodedTextBox = Text(DecodedTextBoxTitle,wrap='none', undo=True, autoseparators=True,borderwidth=5, relief="sunken",selectbackground="dark red")
DecodedTextBox.config(highlightbackground="gray15")
DecodedTextBox.config(foreground="red")
DecodedTextBox.config(highlightthickness=3)
DecodedTextBox.config(highlightcolor="dark red")
DecodedTextBox.config(background="gray15")
DecodedTextBox.pack(fill="both", expand=True)
DecodedTextBox.configure(state='normal')
If you want to use pack, then you should create a frame just for the buttons. You can then use pack to put the frame on top and the LabelFrame below.
Or, you can switch to using grid so that you can put each button in the same row but a different column.

Python Tkinter Button not appearing with grid

I'm trying to program a little calculator in python3 with Tkinter. When I try to organize my buttons and the input filed with grid(), my buttons are not appearing. I am sure I forgot something but hopefully someone can find my issue.
from tkinter import *
gleichung = ""
def zahl_gedrückt(num):
global gleichung
gleichung = gleichung + str(num)
gleichung_text.set(gleichung)
if __name__ == "__main__":
app = Tk()
app.title("Taschenrechner")
app.geometry("400x600")
gleichung_text = StringVar()
gleichung_text.set('Gleichung...')
gleichung_textbox = Entry(app, font=("Calibri 25"), state='disabled', width=400, justify='center', textvariable=gleichung_text)
gleichung_textbox.grid(row=0, columnspan=4)
button1 = Button(app, text=' 1 ', font=("Calibri 40"), command=lambda: zahl_gedrückt(1)).grid(row=2, column=0)
button2 = Button(app, text=' 2 ', font=("Calibri 40"), command=lambda: zahl_gedrückt(2)).grid(row=2, column=1)
app.mainloop()

Python3 and Tkinter, I can not get radio buttons to change value of a variable.

trying to write a program to move mouse with the keyboard and record locations on gui buttons on my screen. i'm using a variable named speed as way to move the mouse across the screen faster. why can't i get the variable speed to changed when I select the different radio buttons? Thanks for your help
import pyautogui, readchar
from tkinter.ttk import *
import tkinter as tk
import time
#rootsetup
root=tk.Tk()
root.title("GUI Mapping Tool")
root.geometry('650x200')
CurrentPostionX, CurrentPostionY= pyautogui.position()
#Circle or Rectangle radio
Circle = Radiobutton(root,text='Circle', value=101)
Rectangle = Radiobutton(root,text='Rectangle', value=102)
Circle.grid(column=4, row=1)
Rectangle.grid(column=3, row=1)
#Speed radio
speedchoice=tk.IntVar()
def selected():
if speedchoice.get()==1:
Speed=1
print("1")
if speedchoice.get()==10:
Speed=10
print("10")
if speedchoice.get()==25:
Speed=25
print("25")
if speedchoice.get()==100:
Speed=100
print("100")
Speed1 = Radiobutton(root,text='1x', value=1 , command=selected, variable= speedchoice)
Speed10 = Radiobutton(root,text='10x', value=10, command=selected, variable= speedchoice )
Speed25 = Radiobutton(root, text='25x', value=25, command=selected, variable= speedchoice )
Speed100 = Radiobutton(root,text='100x', value=100, command=selected, variable= speedchoice )
Speed1.grid(column=4, row=0)
Speed10.grid(column=5, row=0)
Speed25.grid(column=6, row=0)
Speed100.grid(column=7, row=0)
#MoveMouseWithArrows:
def key(event):
CurrentPostionX, CurrentPostionY= pyautogui.position()
Direction=event.keysym
if Direction=='Up':
pyautogui.moveTo(CurrentPostionX, CurrentPostionY-1*Speed)
if event.keysym =='Down':
pyautogui.moveTo(CurrentPostionX, CurrentPostionY+1*Speed)
if event.keysym =='Left':
pyautogui.moveTo(CurrentPostionX-1*Speed, CurrentPostionY)
if event.keysym =='Right':
pyautogui.moveTo(CurrentPostionX+1*Speed, CurrentPostionY)
root.bind_all('<Key>', key)
# Locations Name Entry
txt = Entry(root,width=20)
txt.grid(column=3, row=0)
CurrentPostionX, CurrentPostionY= pyautogui.position()
Xpos=tk.StringVar()
Ypos=tk.StringVar()
def update_label():
while 1>0:
CurrentPostionX, CurrentPostionY= pyautogui.position()
Xpos.set(str(CurrentPostionX))
Ypos.set(str(CurrentPostionY))
root.update()
Xaxislabel=tk.Label(root, text= "X:", font=("Arial Bold", 50))
Xaxislabel.grid(column=0, row=0)
Xposlabel=tk.Label(root,textvariable=Xpos, font=("Arial Bold", 50))
Xposlabel.grid(column=1, row=0)
Yaxislabel=tk.Label(root, text= "Y:", font=("Arial Bold", 50))
Yaxislabel.grid(column=0, row=1)
Yposlabel=tk.Label(root,textvariable=Ypos, font=("Arial Bold", 50))
Yposlabel.grid(column=1, row=1)
update_label()
root.mainloop()

How to get a value from Scale/Slider which generates a button in Python tkinter

I have a button that when clicked displays slider / scale. How to get the selected value from slider?
My code returns error: "TypeError: funcSlider() missing 1 required positional argument: 'value'"
self.list = {}
def onClick(self):
print (self.list)
return
def funcSlider(self, posX,posY,key,value):
w = tk.Scale(self, from_=0, to=8, orient='horizontal')
w.pack()
w.place(x=posX, y=posY, in_=self)
self.list [key] = value
button = tk.Button(self, text="button ",
command=lambda: self.funcSlider(120,33,'test'),width = 20)
button .pack(anchor="w")
button .place(x=250, y=50, in_=self)
button2 = tk.Button(self, text="Send",
command=self.onClick, width=42,bg="#4BAD2E")
button2 .pack(anchor="w")
button2 .place(x=250, y=430, in_=self)
I think I did it. I do not know if it is a 'good practice', but it works :D
self.list = {}
def foo(self,x,key):
self.list [key] = x
def onClick(self):
print (self.list)
return
def funcSlider(self, posX,posY,key):
w = tk.Scale(self, from_=0, to=8, orient='horizontal',command=lambda s: self.foo(s,key))
w.pack()
w.place(x=posX, y=posY, in_=self)
self.list [key] = value
button = tk.Button(self, text="button ",
command=lambda: self.funcSlider(120,33,'test'),width = 20)
button .pack(anchor="w")
button .place(x=250, y=50, in_=self)
button2 = tk.Button(self, text="Send",
command=self.onClick, width=42,bg="#4BAD2E")
button2 .pack(anchor="w")
button2 .place(x=250, y=430, in_=self)

Tkinter listbox entry pagination

Recently, I tried to find a way to paginate through listbox entries that runs functions that opens frames with forms, tabs or else, but I didn't find it.
Clearly, I wanna create a control panel application which has a side panel which can switch between pages/frames that hold widgets that user will interact with.
this is the code which I wrote to try to achieve this manner:
import tkinter as tk
from tkinter import ttk
class MainWindow() :
def __init__(self,root):
# menu left
self.menu_upper_frame = tk.Frame(root, bg="#dfdfdf")
self.menu_title_label = tk.Label(self.menu_upper_frame, text="menu title", bg="#dfdfdf")
self.menu_title_label.pack()
self.menu_left_container = tk.Frame(root, width=150, bg="#ababab")
self.menu_left_upper = tk.Frame(self.menu_left_container, width=150, height=150, bg="red")
self.menu_left_upper.pack(side="top", fill="both", expand=True)
# create a listbox of items
self.Lb1 = tk.Listbox(self.menu_left_upper,bg ="red", borderwidth=0, highlightthickness=0 )
self.Lb1.insert(1, "Python")
self.Lb1.insert(2, "Perl")
self.Lb1.insert(3, "C")
self.Lb1.insert(4, "PHP")
self.Lb1.insert(5, "JSP")
self.Lb1.insert(6, "Ruby")
self.Lb1.bind("<<ListboxSelect>>", self.OnClick ) #return selected item
self.Lb1.pack(fill="both", expand=True, pady=50 )
# right area
self.inner_title_frame = tk.Frame(root, bg="#dfdfdf")
self.inner_title_label = tk.Label(self.inner_title_frame, text="inner title", bg="#dfdfdf")
self.inner_title_label.pack()
self.canvas_area = tk.Canvas(root, width=500, height=400, background="#ffffff")
self.canvas_area.grid(row=1, column=1)
# status bar
self.status_frame = tk.Frame(root)
self.status = tk.Label(self.status_frame, text="this is the status bar")
self.status.pack(fill="both", expand=True)
self.menu_upper_frame.grid(row=0, column=0, rowspan=2, sticky="nsew")
self.menu_left_container.grid(row=1, column=0, rowspan=2, sticky="nsew")
self.inner_title_frame.grid(row=0, column=1, sticky="ew")
self.canvas_area.grid(row=1, column=1, sticky="nsew")
self.status_frame.grid(row=2, column=0, columnspan=2, sticky="ew")
root.grid_rowconfigure(1, weight=1)
root.grid_columnconfigure(1, weight=1)
def OnClick(self,event):
widget = event.widget
selection = widget.curselection()
value = widget.get(selection)
# print ("selection: ",selection, ": '%s'"% value)
if value == 'Python':
self.tabtop()
def tabtop(self):
self.tabControl = ttk.Notebook(self.canvas_area, width=400)
self.tab1 = ttk.Frame(self.tabControl)
self.tab2 = ttk.Frame(self.tabControl)
self.tab3 = ttk.Frame(self.tabControl)
self.tab4 = ttk.Frame(self.tabControl)
self.tab5 = ttk.Frame(self.tabControl)
self.tabControl.add(self.tab1, text='Login data' )
self.tabControl.add(self.tab2, text='Permission')
self.tabControl.add(self.tab3, text='Roles')
self.tabControl.add(self.tab4, text='Personal data')
self.tabControl.add(self.tab5, text='Business data')
self.tabControl.pack(expand=1, fill="both")
self.l2 = tk.Label(self.tab2, text="label 2").pack()
self.l3 = tk.Label(self.tab3, text="label 3").pack()
root = tk.Tk()
root.title("Control Panel")
root.style = ttk.Style()
root.style.theme_use("clam")
user = MainWindow(root)
root.mainloop()
If you have an idea to achieve the same manner with a different algorithm please suggest!

Resources