Changing or removing command linked to a button after clicking - python-3.x

Playing around with Tkinter and ran into some trouble.
I have 3 buttons:
Button1 will open my specified website using Selenium.
Button3 will carry out my function on the website.
Button2 will close the program.
Problem: I want to keep button1 from opening multiple windows/websites by
either removing/hiding it after it has been clicked or changing the
command
that will be performed by it.
After some googling I found some options and tried the following:
| pack_forget()
| pack_destroy()
| btn1.destroy()
All of the above does not seem to be doing anything. What am I missing?
import selenium
from selenium import webdriver
from time import sleep
import time
import tkinter
from tkinter import *
main = Tk()
def Primary():
txt = mtxt.get()
New = Label(main, text=""+txt, fg='Blue').pack(side=BOTTOM)
btn1 = Button(Bot, text='OK', command=Primary,
fg='Green').pack_destroy() #For some reason this does not work
return
def Secondary():
global main
main.destroy()
def Tres():
txt = mtxt.get()
New = Label(main, text=""+txt, fg='Blue').pack(side=BOTTOM)
return
main.geometry('350x400+500+300')
main.title('Title')
mtxt = StringVar()
Top = Frame().pack()
Bot = Frame().pack(side=BOTTOM)
Lbl = Label(main, text='TEXT').pack()
Lbl2 = Label(main, text='TEXT').pack()
Enter = Entry(main, textvariable=mtxt).pack()
btn1 = Button(Bot, text='OK', command=Primary, fg='Green').pack()
btn2 = Button(Bot, text='Cancel', command=Secondary,
fg='Red').pack(side=BOTTOM)
btn3 = Button(Bot, text='Next', command=Tres, fg='Green').pack()
main.mainloop()
Expected result:
On initial click btn1 performs 'command=Primary'
After initial click btn1 performs 'command=Tres'
or
after initial click btn1 is destroyed/hidden and btn3 takes it's place.

Related

Create notepad via python

Scope - Python3.10
Looking for -
add multiple editable notes in tabs. (just like notepad)
How to add a save function for a specific note
enter image description here
Attaching work done till now, Help is much appreciated
from tkinter import *
from tkinter import ttk
MainWindow = Tk() #Object#
MainWindow.geometry ('380x250') #size of object#
MainWindow.title("Bnote") #Name of object#
Menubar = Menu(MainWindow)
Appearance = Menu(Menubar,tearoff=0)
NotesArea = Text(MainWindow)
def test(Menubar):
Menubar.entryconfigure(1, label="click")
file_menu = Menu(Menubar, tearoff=False)
file_menu.add_command(label="New_Tab", command=lambda: clicked(file_menu))
Menubar.add_cascade(label="+", menu=file_menu)
scrollbar = Scrollbar(MainWindow) #adding scrollbar#
scrollbar.pack(side=RIGHT,fill=Y) #packing scrollbar#
MainWindow.config(menu=Menubar)
NotesArea = Text(MainWindow,yscrollcommand=scrollbar.set,wrap = WORD, font="Ubuntu 12")
scrollbar.config(command=NotesArea.yview) # configuring scrollbar
NotesArea.pack(fill=BOTH)
MainWindow.mainloop ()

tkinter buttons through a loop, how do i identify which was clicked?

I created a loop to create a button for each element in a list. How do I identify which button is clicked and then assign a command to each one, which will create a new page with the same name as the button clicked?.
yvalue = 200
for i in sdecks:
deckbutton1=Button(fcpage,text=i,width=21,bd=2,fg="ghost white",bg="orchid1")
deckbutton1.place(x=105, y=yvalue)
yvalue = yvalue + 20
Either I don't get you question or this (adapted from here) should answer your question:
from functools import partial
import tkinter as tk
def create_new_page(name_of_page):
print("Creating new page with name=\"%s\"" % name_of_page)
root = tk.Tk()
for i in range(5):
# Create the command using partial
command = partial(create_new_page, i)
button = tk.Button(root, text="Button number"+str(i+1), command=command)
button.pack()
root.mainloop()

How to get input from tkinter Entry widget on second window while the first window continues to run

from tkinter import *
def first():
root1 = Tk()
Button(root1, text = 'get Second', command= second).pack()
root1.mainloop()
def second():
root2 = Tk()
user_input = StringVar()
Entry(root2, text = user_input).pack()
Button(root2, text = 'submit', command = lambda : print(user_input.get(), '\t printed')).pack()
root2.mainloop()
first()
You are making a few basic mistakes in here -
You if want to use a second window, it should be Toplevel not root Tk window. There should be only one root window in the program. This should act as parent to all the windows.
Its a good practice in most of the cases to define the widgets like Button, Entry separately and then pack() them.
Entry should have 'textvariable' not 'text'
Following is the updated code which may help you -
from tkinter import *
root = Tk()
def first():
button = Button(root, text = 'get Second', command= second)
button.pack()
root.mainloop()
def second():
window2 = Toplevel(root)
user_input = StringVar()
entry = Entry(window2, textvariable=user_input)
entry.pack()
button = Button(window2, text = 'submit', command = lambda: print(user_input.get()))
button.pack()
first()

TKinter auto updating label from urllib

I'm trying make auto updating label from url.
I want to make something like pager. When file on server is changed, label should changes too. with button I can download it manually but I want to automate it. Where I'm making mistake?
from tkinter import *
import urllib.request
import time
root = Tk()
check = ""
#functions
def auto():
time.sleep(5) #becouse I don't want kill server
page = "http://howan.pl/pychal/plik.txt"
g = urllib.request.urlopen(page)
data = g.read()
g.close()
return (str(data, encoding='utf-8'))
def click():
page = "http://howan.pl/pychal/plik.txt"
g = urllib.request.urlopen(page)
data = g.read()
g.close()
label.config(text=str(data, encoding='utf-8'))
#Widgets
label = Label(root, text="zer0")
button = Button(root, text="hey", command= click)
if auto() == check:
check = auto
label.config(text=check)
print(auto())
label.pack()
button.pack()
root.mainloop()
To automate it you need to make a function that does the work, and then use root.after() to call that function on a regular basis. Since you have all the work in "click" already, you could just add:
def auto_click():
click()
root.after(5000, auto_click) # call this function again in 5,000 ms (5 seconds)
auto_click() # start the autoclick loop.

Getting function NOT to run on import

I have tried EVERY " if name=='main' " suggestion and it still fails (or maybe I'm doing it wrong or not understanding it). I am also Not trying to run things from a command line. While testing things out in Pycharm, I am trying to get the main file to pop-up with a gui and then when I click on the button, trying to get the first window to go away and the second file/gui to pop up in its place.
Any help would be greatly appreciated.
this is the main one I am trying to start with but opens up with the second gui first
from tkinter import *
from tryingstuff import EmdMain
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
#firebutton
self.fphoto = PhotoImage(file="../icon/fireorig.png")
self.fbutton = Button(frame, image=self.fphoto)
self.fbutton.config( height=228, width=200)
self.fbutton.pack(side=LEFT)
#emsbutton
self.mphoto = PhotoImage(file="../icon/ems.png")
self.emsButton = Button(frame, image=self.mphoto, command=EmdMain)
self.emsButton.config( height=224, width=197)
self.emsButton.pack(side=RIGHT)
root = Tk()
root.title("ProQA Card set")
app = App(root)
root.mainloop()
This is the second file
from tkinter import *
def EmdMain():
frame = Frame()
frame.pack()
abdominalPnB = Button(frame, text="01_Abdominal Pain")
abdominalPnB.config(anchor="w", width=20, height=1)
abdominalPnB.grid(row=0, column=0)
root = Tk()
app = EmdMain()
root.mainloop()
if __name__=='__main__':
EmdMain()

Resources