How to set default text for a Tkinter Entry widget - python

How do I set the default text for a Tkinter Entry widget in the constructor? I checked the documentation, but I do not see a something like a "string=" option to set in the constructor?
There is a similar answer out there for using tables and lists, but this is for a simple Entry widget.

Use Entry.insert. For example:
try:
from tkinter import * # Python 3.x
except Import Error:
from Tkinter import * # Python 2.x
root = Tk()
e = Entry(root)
e.insert(END, 'default text')
e.pack()
root.mainloop()
Or use textvariable option:
try:
from tkinter import * # Python 3.x
except Import Error:
from Tkinter import * # Python 2.x
root = Tk()
v = StringVar(root, value='default text')
e = Entry(root, textvariable=v)
e.pack()
root.mainloop()

For me,
Entry.insert(END, 'your text')
didn't worked.
I used Entry.insert(-1, 'your text').
Thanks.

Related

I have a weird problem with tkinter when using python

I am trying to learn how to use Tkinter, but whenever I want to execute my code I always get this problem: (NameError: name 'label' is not defined) or (NameError: name 'button' is not defined).
As a beginner, it seems to me that the problem is with my code editor. (BTW I am using VScode)
this is my code:
from tkinter import *
root = Tk()
mylabel = label(root, text='Hello World')
mylabel.pack()
root.mainloop()
And as I said, this also happens with this one:
from tkinter import *
root = Tk()
mybutton = button(root, text='Hello World')
mybutton.pack()
root.mainloop()
you have caps error its Button and Label not button and label

How to add multiple choices with tkinter ttk?

import os
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
root = Tk()
variable = StringVar(root)
variable = StringVar(root)
variable.set('GB')
w = Combobox(root, values = choices)
w.pack(); root.mainloop()
choices = ['']
msedgelocation= 'my msedge location'
chromelocation= 'my chrome location'
if os.path.exists(msedgelocation):
# add msedge to choices
else:
if os.path.exists(chromelocation):
# add chrome to choices
This will only add 1 option, How do I change the script so it adds multiple options?
Preferably just code and an explanation, but anything helps. Thanks!
EDIT: Also a submit box would be nice
I improved Your code a bit:
from tkinter import Tk, StringVar
from tkinter.ttk import Combobox
import os
def get_choice(event):
os.startfile(choices[event.widget.get()])
root = Tk()
variable = StringVar()
variable.set('GB')
choices = {'GB': 'gb.exe', 'AUS': 'aus.exe', 'USA': 'usa.exe', 'Chrome': 'chrome.exe'}
c_box = Combobox(root, values=list(choices.keys()), textvariable=variable, state='readonly')
c_box.pack()
c_box.bind('<<ComboboxSelected>>', get_choice)
root.mainloop()
First of I got all the info about the combobox here where it was put in simple words.
The other thing is that You should name variables with meaningful variable names not just one letter names, especially if You plan on using the variable.
Also as I mentioned in the comments, it is suggested that You import only what You need, as You can see in my provided code.
Lastly, as #acw1668 already mentioned in comments the code after .mainloop() will only get executed when the window closes.

how to make modern button in tkinter

I want to have abutton like this in tkinter (Modern window 10 buttons):
However, I get this button:
The code for my button is:
from tkinter import Tk,Button
root=Tk()
Button(root,text='OK').pack()
Another alternative to create button is to create a label and bind it to the action functions. In the below example .bind() is used to connect the label with respective function. You can design according to your requirements.
from tkinter import *
def OnPressed(event):
print('Hello')
def OnHover(event):
But.config(bg='red', fg='white')
def OnLeave(event):
But.config(bg='white', fg='black')
root = Tk()
But = Label(root, text='Hi', bg='white', relief='groove')
But.place(x=10, y=10, width=100)
But.bind('<Button-1>', OnPressed)
But.bind('<Enter>', OnHover)
But.bind('<Leave>', OnLeave)
root.mainloop()
The themed widgets are in 'themed Tk' aka ttk.
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
ok = ttk.Button(root, text='OK')
ok.pack()
root.mainloop()
Avoid from tkinter import * as both tkinter and tkinter.ttk define Button and many other widgets.
If you use this on Windows you should get something looking like a native button. But this is a theme and can be changed. On Linux or MacOS you will get a button style that is appropriate to that platform.
vol_up = Button(root, text="+",activebackground='orange',bg='yellow')
vol_up.pack(side='top')

Changing source file while program is running

I am trying to change a program's behaviour on the fly by editing its source file (under Python 3.4.2, Windows 8.1). My program is a tkinter GUI with a button that sums two values, but I want to be able to change the button's behaviour. I am currently trying to do this by editing the source file (changing, say, the addition to subtraction), saving it, and then clicking a button whose callback function imports the source file. I want my code changes to be reflected in the running GUI without having to exit and restart the program. I also want this import to only recompile the lines that I changed, rather than the entire file.
The program, reload0.py:
import time
import serial
import sys
import os
import tkinter as tk
from tkinter import ttk
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
try:
import Tkinter # Python 2
import ttk
except ImportError:
import tkinter as Tkinter # Python 3
import tkinter.ttk as ttk
mGui = Tk()
mGui.title("GUI")
mGui.geometry('400x200+100+100')
def mOp():
num1 = value1.get()
num2 = value2.get()
Op=num1+num2
name1.set('Sum')
name2.set(Op)
def mReLoad():
import reload0.py
mGui.update()
def mCheck():
if len(name1.get()) == 0:
name1.set('name1')
mGui.update()
if (len(name2.get()) == 0):
name2.set('name2')
mGui.update()
try:
print(value1.get())
except ValueError:
value1.set(0)
mGui.update()
try:
print(value2.get())
except ValueError as ValE:
value2.set(0)
mGui.update()
print(ValE)
value1 = DoubleVar()
value2 = DoubleVar()
name1 = StringVar()
name2 = StringVar()
mButtonSave = Button(mGui, text = "Operation", command = mOp, fg = 'Red').place(x=150,y=80)
mButtonLoad = Button(mGui, text = "ReLoad Operation", command = mReLoad, fg = 'Red').place(x=150,y=110)
mButtonLoad = Button(mGui, text = "Check", command = mCheck, fg = 'Red').place(x=150,y=140)
tText1 = Entry(mGui, textvariable = name1).place(x=10,y=80)
tText2 = Entry(mGui, textvariable = name2).place(x=10,y=100)
vText1 = Entry(mGui, textvariable = value1).place(x=10,y=120)
vText2 = Entry(mGui, textvariable = value2).place(x=10,y=140)
For your purpose of changing button functionality there are easy ways than changing source code, and as commented it's not possible.
judging from another question i saw of yours you are quite new to python programming. I would recommend spending some time on some basic tutorials getting to know python and some programming concepts first.
for instance
import reload.py
should simply be
import reload
https://wiki.python.org/moin/BeginnersGuide/NonProgrammers
read a book, do some examples, and so enough you will be the one answering the questions here, good luck!

Using tkinter with ibPy

I am trying to use tkinter with ibPy. I am using Spyder (Spyder 2.3.0). When I enter the sample program
from tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
I receive the message:
File "/Users/Ameade/Tkinter.py", line 8, in <module>
from tkinter import *
ImportError: No module named tkinter
Do you know where I can get this module? I am working on a Mac (OSX 10.9.4).
It seems you named your sample program file Tkinter.py. You should change this name to something else and it should work.
EDIT
As Kevin said, give to the file any other name but not the name of a python module (the extension must remain .py), so you could name it my_amazing_program.py. And keep the content of the file the same as you originally posted it - if you're using python 3+:
from tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
If you're using python 2+ change tkinter to Tkinter on the import line:
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
You may need to check the version of python you are using.
import tkinter works on python 3(instead of Tkinter)

Categories