I need to create a choice box where i can click on arrow and it give me list of choices.
And if i click on one of them it will change it in that first rectangle.
Its possible to do something like this?
Thank you for any idea.
You can also try an OptionMenu:
from Tkinter import *
root = Tk()
choices = ['GB', 'MB', 'KB']
variable = StringVar(root)
variable.set('GB')
w = OptionMenu(root, variable, *choices)
w.pack(); root.mainloop()
Or you can try using a Combobox:
from ttk import *
from Tkinter import *
root = Tk()
choices = ['GB', 'MB', 'KB']
variable = StringVar(root)
variable.set('GB')
w = Combobox(root, values = choices)
w.pack(); root.mainloop()
Tkinter has two widgets that do what you want. One is OptionMenu and the other is ttk.Combobox.
import Tkinter as tk
import ttk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
choiceVar = tk.StringVar()
choices = ("choice 1", "choice 2", "choice 3", "choice 4")
choiceVar.set(choices[0])
om = tk.OptionMenu(self, choiceVar, *choices)
cb = ttk.Combobox(self, textvariable=choiceVar, values=choices)
om.pack()
cb.pack()
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()
There is a class that someone made for a drop down list view. Using this class, you can try:
from Tkinter import *
# insert class here
root = Tk()
view = ChoiceBox(root, ['MB', 'KB', 'GB', 'TB'])
view.place_configure(x = 0, y = 0)
root.mainloop()
Related
I have a Listbox and a Entry Widget. If i have selected an item in the Listbox per Bind and now click in the Entry Widget and try to invert a text input, there comes a ListboxSelect Event. How can i avoid this ?
import tkinter as tk
from tkinter import ANCHOR, ttk
from tkinter.messagebox import showinfo
root = tk.Tk()
root.title('Listbox plus Entry')
my_list = ['Adam', 'Bert', 'Claudia', 'Eva', 'Goldi']
var = tk.Variable(value=my_list)
my_Listbox = tk.Listbox(
root,
listvariable=var,
height=6,
selectmode=tk.SINGLE
)
my_Listbox.pack(expand=True, fill=tk.BOTH)
my_word = tk.StringVar()
def item_selected(event):
# selected_indice = my_Listbox.curselection()
# selected_word = my_Listbox.get(selected_indice)
selected_word = my_Listbox.get(ANCHOR)
msg = f'You selected: {selected_word}'
showinfo(title= "Listbox - Info", message=msg)
# my_Listbox.select_clear(0, tk.END)
my_Listbox.bind("<<ListboxSelect>>", item_selected)
my_entry = tk.Entry(root, textvariable=my_word).pack()
root.mainloop()
You can set exportselection=0 in tk.Listbox(...) to disable the clear of selection when there is other selection in other widget:
my_Listbox = tk.Listbox(
root,
listvariable=var,
height=6,
selectmode=tk.SINGLE,
exportselection=0
)
from tkinter import *
import tkinter.ttk as ttk
root = Tk()
root.title("TEST ")
lab_pro_val4=Label(root)
lab_pro_val4=Label(root, text="Anything input", width=30, height=1 )
lab_pro_val4.pack()
ent_pro_val4 = Entry(root)
ent_pro_val4.pack()
def btncmd4_add():
tru = (ent_pro_val4=='TEST123')
print(tru)
btn4_ppid = Button(root, text="Check ", command= btncmd4_add,bg = "white")
btn4_ppid.pack()
root.mainloop()
I used Tkinter but I had some trouble.
My question : why is it diff from entry to str(same)
I type the 'TEST123' in entry box.
But it's False... Why is it different?
Please let me know.
ent_pro_val4 is Entry widget, you need to get value of said widget, which can be done using .get(), that is replace
tru = (ent_pro_val4=='TEST123')
using
tru = (ent_pro_val4.get()=='TEST123')
So basically i want to make this GUI where you can insert text and then hit the go button ad it would do the feature. (example: "Stopwatch" and then after you hit enter it would start a stopwatch).
import tkinter as tk
import math
import time
root = tk.Tk()
root.geometry()
root.attributes("-fullscreen", True)
exit_button = tk.Button(root, text = "Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
main_entry = root.Entry(root, )
root.mainloop()
So basically I want the answer in main entry to be taken and do the action asked in the text insertion.
Here i wrote an example on how to use the Entry and the Button widgets in Tkitner:
from tkinter import *
from tkinter.messagebox import showerror
import random
class App(Tk):
def __init__(self):
super(App, self).__init__()
self.title('Get a random integer')
self.geometry('400x250')
Label(self, text='Get a random number from A to B').pack(pady=20)
self.a_value = Entry(self, width=10)
self.a_value.insert(END, 'A')
self.a_value.pack(pady=5)
self.b_value = Entry(self, width=10)
self.b_value.insert(END, 'B')
self.b_value.pack(pady=5)
self.btn = Button(self, text='Get Random', command=self.get_rand)
self.btn.pack(pady=5)
self.out_label = Label(self)
self.out_label.pack(pady=10)
self.bind('<Return>', self.get_rand)
def get_rand(self, event=None):
a = self.a_value.get()
b = self.b_value.get()
try:
a = int(a)
b = int(b)
self.out_label.configure(text=f'{random.randint(a, b)}')
self.update()
except Exception as error:
showerror(title='ERROR', message=f'{error}')
if __name__ == '__main__':
myapp = App()
myapp.mainloop()
I'm just creating simple window using tkinter which have entry box and search button. What is want is when i maximize window search bar also starch but it is not happening,
Here is my code
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry("500x500")
root.title("Wikipedia")
class first:
def labels(self):
search_frame = ttk.LabelFrame(root)
search_frame.pack(side = "top",fill = "both")
search_var = tk.StringVar()
search_bar = tk.Entry(search_frame,width = 40,textvariable = search_var)
search_bar.grid(row = 0,column = 0)
search_button = ttk.Button(search_frame,text = "Search")
search_button.grid(row = 1,column = 0)
boot = first()
boot.labels()
root.mainloop()
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry("500x500")
root.title("Wikipedia")
class first:
def labels(self):
search_frame = ttk.LabelFrame(root)
search_frame.pack(side="top", fill="both")
search_var = tk.StringVar()
search_bar = tk.Entry(search_frame, width=20, textvariable=search_var)
search_bar.pack(fill="x")
search_button = ttk.Button(search_frame, text="Search")
search_button.pack()
boot = first()
boot.labels()
root.mainloop()
Not sure this is what you're looking for but try it out.
Here I have used fill='x' in the .pack() geometry manager to fill the row
I am looking for a way to change the content of the window based on what option you select in the OptionMenu. It should have 3 different options, namely "Introduction", "Encrypt" and "Decrypt". I've the code to create an OptionMenu but now I wanna know how can I modify them to show a different page, depending on the one who is selected. Could someone help me with that? I am using python 3
so for example:
from tkinter import *
OptionList = [
"Einführung",
"Verschlüsseln",
"Entschlüsseln"
]
window = Tk()
window.geometry('200x200')
variable = StringVar(window)
variable.set(OptionList[0])
opt = OptionMenu(window, variable, *OptionList)
opt.config(width=90, font=('Calbri', 12))
opt.pack(side="top")
window.mainloop()
This will produce a window with a OptionMenu with the three options I wrote above (just in German) and now I'd like to change the page depending on the current chosen option of the OptionMenu
Thanks guys!
This is now the forth edit or something, but thats the final solution i've come up with.
#coding=utf-
import tkinter as tk
from tkinter import *
window = Tk()
window.geometry('200x200')
OptionList = ["Einführung", "Verschlüsseln", "Entschlüsseln"]
class App:
def __init__(self, master):
self.choice_var = tk.StringVar()
self.choice_var.set(OptionList[0])
opt = OptionMenu(window, self.choice_var, *OptionList, command=self.switch)
opt.config(width=90, font=('Calbri', 12))
opt.pack(side="top")
self.random_label1 = tk.Label(window, text="Welcome content here")
self.random_label2 = tk.Label(window, text="Encrypt content here")
self.random_label3 = tk.Label(window, text="Decrypt content here")
self.random_label1.pack()
self.random_label2.pack()
self.random_label3.pack()
self.label_info1 = self.random_label1.pack_info()
self.label_info2 = self.random_label2.pack_info()
self.label_info3 = self.random_label3.pack_info()
self.switch()
def switch(self, *args):
var = str(self.choice_var.get())
if var == "Einführung":
self.random_label1.pack(self.label_info1)
self.random_label2.pack_forget()
self.random_label3.pack_forget()
if var == "Verschlüsseln":
self.random_label2.pack(self.label_info2)
self.random_label1.pack_forget()
self.random_label3.pack_forget()
if var == "Entschlüsseln":
self.random_label3.pack(self.label_info3)
self.random_label2.pack_forget()
self.random_label1.pack_forget()
myApp = App(window)
window.mainloop()