Simple dialogbox multitasking - python

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()

Related

I need to know how to exec two commands on the same var with Tkinter

from tkinter import *
class Janela:
def __init__(self, master):
self.master = master
self.master.geometry('800x480-3+0')
self.start = Button(text="Start", font="minecraftia 18", bg="grey", command=self.menu)
self.start.place(x=364, y=290)
def menu(self):
jan1 = Tk()
self.bt1 =Button(jan1, text = "next", command=self.jans)
self.bt1.place(x=200, y=200)
jan1.geometry("800x480-3+0")
def jans(self):
jan2 = Tk()
self.bt2 =Button(jan2, text="back", command=jan2.destroy, command=jan1.destroy)
self.bt2.place(x=200, y=200)
jan2.geometry("800x480-3+0")
root = Tk()
Janela(root)
root.mainloop()
A rather simple solution will be to create a function_A that calls 2 functions or methods. And then bind that function to the Tkinter button.
def function_A(func1, func2):
func_1() # or method
func_2() # or method
So in your function
def jans(self):
jan2 = Tk()
def double_command():
jan2.destroy()
jan1.destroy()
self.bt2 =Button(jan2, text="back", command=double_command)
self.bt2.place(x=200, y=200)
jan2.geometry("800x480-3+0")
and in Tkinter button you can
self.bt2 =Button(jan2, text="back", command=double_command)

How Transfer cursor in tkinter?

i wrote bellow code in python 3.6.2 by tkinter,I want the cursor move to password textbox when user press Enter key in username textbox.
from tkinter import *
class Application(Frame):
def __init__(self,master):
super(Application, self).__init__(master)
self.grid()
self.create_main()
def create_main(self):
print("testing")
self.title = Label(self, text=" Stuck In The Circle ")
self.title.grid(row=0, column=2)
self.user_entry_label = Label(self, text="Username: ")
self.user_entry_label.grid(row=1, column=1)
self.user_entry = Entry(self)
self.user_entry.grid(row=1, column=2)
self.pass_entry_label = Label(self, text="Password: ")
self.pass_entry_label.grid(row=2, column=1)
self.pass_entry = Entry(self)
self.pass_entry.grid(row=2, column=2)
self.user_entry = Entry(self, justify="right")
self.pass_entry = Entry(self, justify="right")
self.sign_in_butt = Button(self, text="Sign In",command = self.logging_in)#SIGN IN BUTTON
self.sign_in_butt.grid(row=5, column=2)
def logging_in(self):
user_get = self.user_entry.get()
pass_get = self.pass_entry.get()
root = Tk()
root.title("Stuck in the Circle")
root.geometry("400x100")
app = Application(root)
root.mainloop()
How can do it?
This is actually a lot simpler than I expected it to be.
We can use .bind() to get a callback on the <Return> event. This means that every time the return character is pressed whenever the defined widget is in focus we get a callback.
In order to get it to cycle to the next widget we can use this answer from Bryan Oakley:
def focus_next_window(event):
event.widget.tk_focusNext().focus()
return("break")
text_widget=Text(...) text_widget.bind("<Tab>", focus_next_window)
Important points about this code:
The method tk_focusNext() returns the next widget in the keyboard
traversal hierarchy. the method focus() sets the focus to that widget
returning "break" is critical in that it prevents the class binding
from firing. It is this class binding that inserts the tab character,
which you don't want.
So, applying the same logic in our situation we can use something like the below:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.entry1 = Entry(self.root)
self.entry2 = Entry(self.root)
self.entry1.pack()
self.entry2.pack()
self.entry1.bind("<Return>", self.callback)
def callback(self, event):
event.widget.tk_focusNext().focus()
root = Tk()
App(root)
root.mainloop()

Display selected options from checkbox to a new list in next tkinter window

I want to display the selected options from my checkbox list in a new list in a new Tkinter window and when browsing it comes back to the main screen
(using Python 3.5 with Ubuntu 16.04).
import tkinter as tk
import tkMessageBox
lista=['jpeg','jfit','tiff','gif','png','bmp']
class PopUp(tk.Toplevel):
def __init__(self, number=10):
tk.Toplevel.__init__(self)
self.global_state = tk.BooleanVar()
cb = tk.Checkbutton(self, text="select/deselect all", variable=self.global_state, command=self.select_clear_states)
cb.grid(row=0, column=0, padx=5, pady=1)
self.states = []
for n in range(len(lista)):
var = tk.BooleanVar()
cb = tk.Checkbutton(self, text=str(lista[n]), variable=var)
cb.grid(row=n+1, column=0, padx=5, pady=1)
self.states.append(var)
def select_clear_states(self):
state = self.global_state.get()
for x in self.states:
x.set(state)
def popup(num):
win = PopUp(num)
root = tk.Tk()
b = tk.Button(root, text="5 checkboxes", command=lambda:popup(5))
b.pack()
root.mainloop()
Assuming I understand you correctly, if you wanted to do this with Checkbuttons you could do something like the below:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.top = Toplevel(root)
self.frame = Frame(self.top)
self.frame.pack()
self.check = []
self.var = []
for i in range(10):
self.var.append(IntVar())
self.var[i].trace("w", self.callback)
self.check.append(Checkbutton(self.root, text="Option "+str(i), variable=self.var[i]))
self.check[i].pack()
def callback(self, *args):
self.frame.destroy()
self.frame = Frame(self.top)
self.frame.pack()
for i, c in zip(self.check, self.var):
if c.get() == 1:
Label(self.frame, text=i.cget("text")).pack()
root = Tk()
App(root)
root.mainloop()
Alternatively you might find that a Listbox accomplishes what you want and (IMO) looks a lot cleaner and more user friendly:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.listbox = Listbox(self.root, selectmode=MULTIPLE)
self.listbox.pack()
for i in range(10):
self.listbox.insert(END, "Option "+str(i))
root = Tk()
App(root)
root.mainloop()
Using the Listbox you probably wouldn't even need two windows as the selection quite clearly shows which options are selected by highlighting them.

Python tkinter checkbutton value not accessible

I want to build a little GUI application in Python. The goal is to have a main window calling several other windows. In one of these called windows I have a checkbutton. My problem is that I cannot read the value of this checkbutton, whereas I can read the value of an Entry widget. What am I doing wrong?
from tkinter import *
import tkinter as tk
class mainwindow():
def __init__(self, master):
self.master = master
menubalk = Menu(self.master)
menubalk.add_command(label="New window", command=self.openNewwindow)
self.master.config(menu=menubalk)
def openNewwindow(self):
window = newwindow()
window.mainloop()
class newwindow(Tk):
def __init__(self):
Tk.__init__(self)
self.var = BooleanVar()
self.checkbutton = Checkbutton(self, text="Check", variable=self.var)
self.checkbutton.grid(column=0, row=0)
self.var2 = StringVar()
self.entry = Entry(self, textvariable=self.var2)
self.entry.grid(column=2,row=0)
self.button2 = Button(self,text=u"Show", command=self.showValues).grid(column=1, row=0)
def showValues(self):
print('Value checkbutton:', self.var.get(), ';', 'Value entryfield: ', self.entry.get())
def main():
root = Tk()
window = mainwindow(root)
root.mainloop()
if __name__ == '__main__':
main()
You are making multiple, separate Tkinter applications in your program. Do not do that. To create new windows, use the Toplevel widget.
from tkinter import *
class mainwindow():
def __init__(self, master):
self.master = master
menubalk = Menu(self.master)
menubalk.add_command(label="New window", command=self.openNewwindow)
self.master.config(menu=menubalk)
def openNewwindow(self):
def showValues(var, entry):
print('Value checkbutton:', var.get(), ';', 'Value entryfield: ', entry.get())
window = Toplevel(self.master)
var = BooleanVar()
checkbutton = Checkbutton(window, text="Check", variable=var)
checkbutton.grid(column=0, row=0)
var2 = StringVar()
entry = Entry(window, textvariable=var2)
entry.grid(column=2,row=0)
button2 = Button(window,text=u"Show", command=lambda: showValues(var, entry))
button2.grid(column=1, row=0)
def main():
root = Tk()
window = mainwindow(root)
root.mainloop()
if __name__ == '__main__':
main()
Tkinter's variable objects (IntVar, StringVar, etc.) must take argument "master" as their firs parameter. i.e. replace
self.var=StringVar()
With
self.var=StringVar(self)
Or
self.var=StringVar(master=self)

Tkinter (python 2.7.2) Help needed

I'm trying to make my first GUI program. The problem is, that I can't figure out how to make a main menu, which would switch to one of the programs after clicking a button.
#Dev by Mkee
from Tkinter import *
import tkMessageBox
#Main Stuff
app = Tk()
app.title("Mkee's Tools")
app.geometry('300x200')
#modules
class Programs:
def Shuffle():
app2 = Tk()
app2.title("Shuffle")
app2.geometry('300x200')
app2.mainloop()
#end of modules
labelText = StringVar()
labelText.set('')
label1 = Label(app, textvariable=labelText, height=4)
label1.pack()
button1 = Button(app, text='Shuffle', width=30, command=Programs.Shuffle)
button1.pack(side='right', padx=5,pady=1)
app.mainloop()
I know that I'm doing it wrong. I just have no idea how to do it, So i gave it a try of how could it be. Please help me.
You could call pack_forget() to hide widgets, and (later)
pack to show them again:
Tkinter is singled-threaded, and mainloop runs the main event loop. Therefore you shouldn't call mainloop twice.
#Dev by Mkee
import Tkinter as tk
import sys
class Shuffle(object):
def __init__(self,master=None):
self.master=master
self.text=tk.Text(master)
def hide(self):
self.text.pack_forget()
def show(self):
self.text.pack(side=tk.LEFT, padx=5, pady=5)
class Buttons(object):
def __init__(self,master=None):
self.master=master
self.red = tk.Button(self.master, text="Red", bg="red", fg="white")
self.green = tk.Button(self.master, text="Green", bg="green", fg="black")
self.blue = tk.Button(self.master, text="Blue", bg="blue", fg="white")
def hide(self):
self.red.pack_forget()
self.green.pack_forget()
self.blue.pack_forget()
def show(self):
self.red.pack(side=tk.LEFT,expand=tk.YES,fill=tk.BOTH)
self.green.pack(side=tk.LEFT,expand=tk.YES,fill=tk.BOTH)
self.blue.pack(side=tk.LEFT,expand=tk.YES,fill=tk.BOTH)
class MainApp(object):
def __init__(self,master=None):
self.master=master
app=self.app=tk.Tk()
app.title("Mkee's Tools")
app.geometry('300x200')
self.shuffle=Shuffle(master)
self.buttons=Buttons(master)
self.current=None
menubar=tk.Menu(app)
program_menu=tk.Menu(menubar)
program_menu.add_command(label='Shuffle',
command=lambda: self.show(self.shuffle))
program_menu.add_command(label='Buttons',
command=lambda: self.show(self.buttons))
program_menu.add_command(label='Quit',command=sys.exit)
menubar.add_cascade(label='Programs', menu=program_menu)
app.config(menu=menubar)
def show(self,obj):
if self.current != obj:
try: self.current.hide()
except AttributeError: pass
self.current=obj
obj.show()
def main():
m=MainApp()
m.app.mainloop()
if __name__=='__main__':
main()

Categories

Resources