How can i stop button tkinter python? - python

When i click two timesWhen i click on button the function run again and again i w ant to stop the button from repeat it until i go to another window and make it work again with out repeat, please try the code for more understanding
from tkinter import *
from tkinter.ttk import Treeview
class App_Win():
def __init__(self, main_Win):
self.main_Win = main_Win
self.main_Win.geometry("1100x650+50+50")
self.mainFrame = Frame(self.main_Win,background="white")
self.mainFrame.grid(row=1, column=1, sticky="news")
self.leftFrame = Frame(self.main_Win, background="#007094", width=100)
self.leftFrame.grid(row=0, column=0,rowspan=3, sticky="nws")
self.cusBtn = Button(self.leftFrame, text="Customers", cursor="hand",bg="#007094", bd=4,activebackground="#007094", font=("arial",14), command=self.customers)
self.cusBtn.grid(row=3,column=1,pady=5, sticky="NWES")
def customers(self):
self.customer = Treeview(self.mainFrame,columns=("First Name","Last Name","Phone","Sec Phone","Email","Co Name"))
self.customer["show"]="headings"
self.customer.heading("First Name", text="Firest Name")
self.customer.pack()
root2 = Tk()
App_Win(root2)
root2.mainloop()

Related

How can I have a tkinter popup message with yes or no button and has a dialog box on the same popup message?

I want to create a simple tkinter GUI that popes up a message with YES or NO buttons but also has a dialog box.
import tkinter as tk
from tkinter import simpledialog
messagebox.askyesno("test", "Did you enter your name?")
my_var = simpledialog.askstring(title="Test",prompt="enter sentences:")
print(myvar)
root=tk.TK()
I don't want to have two popups I want to have only one with a dialog box big enough to type three sentences and has a yes or no button. Is there a way to accomplish this in python3?
Below is an example to build a custom dialog using simpledialog.Dialog:
import tkinter as tk
from tkinter.simpledialog import Dialog
class MyDialog(Dialog):
# override body() to build your input form
def body(self, master):
tk.Label(master, text="Enter sentences:", anchor="w").pack(fill="x")
self.text = tk.Text(master, width=40, height=10)
self.text.pack()
# need to return the widget to have first focus
return self.text
# override buttonbox() to create your action buttons
def buttonbox(self):
box = tk.Frame(self)
# note that self.ok() and self.cancel() are defined inside `Dialog` class
tk.Button(box, text="Yes", width=10, command=self.ok, default=tk.ACTIVE)\
.pack(side=tk.LEFT, padx=5, pady=5)
tk.Button(box, text="No", width=10, command=self.cancel)\
.pack(side=tk.LEFT, padx=5, pady=5)
box.pack()
# override apply() to return data you want
def apply(self):
self.result = self.text.get("1.0", "end-1c")
root = tk.Tk()
root.withdraw()
dlg = MyDialog(root, title="Test")
print(dlg.result)
root.destroy()
And the output:

Simple dialogbox multitasking

How do ask a string and a integer question in a single simpledialogbox in tkinter without opening another simpledialogbox
from tkinter import *
from tkinter import simpledialog
simpledialog. askstring("name", "what is your name ")
Mainloop()
You could use Toplevel() to create your own version of a simpledialog, something like the below:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.button1 = Button(self.root, text="Ok", command=self.drawtop)
self.button1.pack()
def drawtop(self):
self.top = Toplevel(root)
self.entry1 = Entry(self.top)
self.entry2 = Entry(self.top)
self.button2 = Button(self.top, text="Done", command=self.printtop)
self.entry1.pack()
self.entry2.pack()
self.button2.pack()
def printtop(self):
print(self.entry1.get())
print(self.entry2.get())
self.top.destroy()
root = Tk()
App(root)
root.mainloop()

Tkinter with Python [duplicate]

This question already has an answer here:
How do I create child windows with Python tkinter?
(1 answer)
Closed 5 years ago.
I am quite new to Python and started learning Tkinter yesterday. I am creating a banking system and I have a Menu made out of the buttons. The problem that I have is that I do not know how to open an additional window when the button is clicked. I have tried with top=Toplevel(), but it only opens two windows on top of each other. What I need is for the additional window to open only when it is activated with a button(event). Can someone help me because I am really stuck as I have about six buttons?
My code so far is:
root.minsize(width=400, height=400)
root.maxsize(width=400, height=400)
root.configure(background='#666666')
label = Frame(root).pack()
Lb = Label(label, text='Welcome to Main Menu',bg='#e6e6e6', width=400).pack()
menu = Frame(root,).pack()
btn_1 = Button(menu, text='Make Deposit', width=15, height=2).pack(pady=5)
btn_2 = Button(menu, text='Withdrawal', width=15, height=2).pack(pady=5)
btn_3 = Button(menu, text='Accounts', width=15, height=2).pack(pady=5)
btn_4 = Button(menu, text='Balance', width=15 ,height=2).pack(pady=5)
btn_5 = Button(menu, text='Exit', width=15, height=2).pack(pady=5)
root.mainloop()
Thank you for the help in advance
Maybe this could work:
from Tkinter import *
class firstwindow:
def __init__(self):
#Variables
self.root= Tk()
self.switchbool=False
#Widgets
self.b = Button(self.root,text="goto second window",command= self.switch)
self.b.grid()
self.b2 = Button(self.root,text="close",command= self.root.quit)
self.b2.grid()
self.root.mainloop()
#Function to chage window
def switch(self):
self.switchbool=True
self.root.quit()
self.root.destroy()
class secondwindow:
def __init__(self):
self.root= Tk()
self.b2 = Button(self.root,text="close",command= self.root.quit)
self.b2.grid()
self.root.mainloop()
one = firstwindow()
if one.switchbool:
two = secondwindow()
Take a look to the Tkinter reference. Check all universal methods and widgets methods. You can do anything you want.
Below is a small example of how you can open a window by clicking on a button. When the user clicks on the button, the function open_window, which was passed to the command option of the button, is called.
import tkinter as tk
def open_window():
top = tk.Toplevel(root)
tk.Button(top, text='Quit', command=top.destroy).pack(side='bottom')
top.geometry('200x200')
root = tk.Tk()
tk.Button(root, text='Open', command=open_window).pack()
tk.Button(root, text='Quit', command=root.destroy).pack()
root.mainloop()

new window in python 3 and tkinter by clicking on button

This is program which I made for now, but I have problem...
how can I make when I click on button1 that then opens new window
import sys
from tkinter import *
import tkinter as tk
def mhello1():
mlabel = Label(mGui, text='A1').pack()
def mhello2():
mlabel = Label(mGui, text='A2').pack()
def mhello3():
mlabel = Label(mGui, text='A3').pack()
def mhello4():
mlabel
return
def mAbout():
messagebox.showinfo(title="About",message="program")
return
def mQuit():
mExit = messagebox.askyesno(title="Quit",message="y/n")
if mExit > 0:
mGui.destroy()
return
mGui = Tk()
mGui.geometry('450x450+200+200')
mGui.title('program')
mGui.configure(bg='gray')
mlabel = Label(text='option:',fg='red',bg = 'blue').pack()
mbutton1 = Button(mGui,text ='Button1',command = mhello1, height=5, width=20).pack()
mbutton2 = Button(mGui,text ='Button2',command = mhello2, height=5, width=20).pack()
mbutton3 = Button(mGui,text ='Button3',command = mhello3, height=5, width=20).pack()
mbutton4 = Button(mGui,text ='Button4',command = mhello4, height=5, width=20).pack()
mlabel2 = Label(text='activity:',fg='red',bg = 'blue').pack()
menubar=Menu(mGui)
filemenu = Menu(menubar, tearoff = 0)
filemenu.add_command(label="qwer")
filemenu.add_command(label="quit",command = mQuit)
menubar.add_cascade(label="more options",menu=filemenu)
helpmenu = Menu(menubar, tearoff = 0)
helpmenu.add_command(label="Help Docs")
helpmenu.add_command(label="About", command = mAbout)
menubar.add_cascade(label="help",menu=helpmenu)
mGui.config(menu=menubar)
mGui.mainloop()
I try this program but it doesn't work:
Python 3 and tkinter opening new window by clicking the button
is there a way that I don't use tkinter toplevel?
Tnx a lot :)
Since you should create only one root window, you have to use a Toplevel to open a new one.
def mhello1():
toplevel = Toplevel()
toplevel.title('Another window')
toplevel.focus_set()
if you wanna use messagebox use those line below
from tkinter import *
from tkinter import messagebox

Python 3 Error: "NameError: global name 'entryWidget' is not defined"

I get the error "NameError: global name 'entryWidget' is not defined" in the following piece of Python 3 tkinter code:
from tkinter import *
import tkinter
def displayText():
tkinter.messagebox.showinfo("Tkinter Entry Widget", "Text value =" + entryWidget)
class Application(Frame):
def createWidgets(self):
root.title("TITLE")
textFrame = Frame(root)
entryLabel = Label(textFrame)
entryLabel["text"] = "Enter the text:"
entryLabel.pack(side=LEFT)
entryWidget = Entry(textFrame)
entryWidget["width"] = 50
entryWidget.pack(side=LEFT)
textFrame.pack()
button = Button(root, text="Submit", command=displayText)
button.pack()
self.QUIT = Button(self)
self.QUIT["text"] = "QUIT"
self.QUIT["fg"] = "red"
self.QUIT["command"] = self.quit
self.QUIT.pack()
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()
The code itself is designed to create a text box (with text next to it), an enter button for the text box and a quit button. When text is entered in the text box and enter is pressed then a message box should appear telling the user what they entered. The quit button just quits the script.
How can I fix the error I am getting (I am aware what the error means, just don't know how to fix it in this scenario). Note that I am new to tkinter and GUI code.
What about something like that:
from tkinter import *
import tkinter
class Application(Frame):
def createWidgets(self):
root.title("TITLE")
textFrame = Frame(root)
entryLabel = Label(textFrame)
entryLabel["text"] = "Enter the text:"
entryLabel.pack(side=LEFT)
self.entryWidget = Entry(textFrame)
self.entryWidget["width"] = 50
self.entryWidget.pack(side=LEFT)
textFrame.pack()
button = Button(root, text="Submit", command=self.displayText)
button.pack()
self.QUIT = Button(self)
self.QUIT["text"] = "QUIT"
self.QUIT["fg"] = "red"
self.QUIT["command"] = self.quit
self.QUIT.pack()
def displayText(self):
tkinter.messagebox.showinfo("Tkinter Entry Widget", "Text value =" + self.entryWidget.get())
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()
I don't have tkinter here so I can't check it. But I hope it will help.
Move definition of displayText into the createWidgets method.

Categories

Resources