I've looked into multiple threads and I can't seem to find out why the stringVar is not displaying on the entry box, once the file has been selected. I can see the print(filePath.get()) has the selected file but entry box stays blank.
Can someone please help?
#import libraries
import os
import paramiko
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter.filedialog import askopenfilename
import sys
def getFilePath():
file_selected = filedialog.askopenfilename(title="Select File")
filePath.set(file_selected)
print(filePath.get())
#-------------------------------GUI------------------------------
gui = tk.Tk()
gui.title("NetWizard 1.0")
tabControl = ttk.Notebook(gui, width = 1000, height = 700)
gui.resizable(False, False)
tabControl.pack( expand = 1, fill ="both")
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
tabControl.add(tab1, text ='RUGGEDCOM', )
tabControl.add(tab2, text ='WESTERMO')
#--------------------------RUGGEDCOM WIDGET----------------------
filePath = StringVar()
entry1 = Entry(gui, textvariable=filePath, width=1000)
entry1 = tk.Entry(tab1, width = 60)
label1 = Label(tab1, text="IP Address List Input:", font=('Arial', '11', 'bold'))
button1 = Button(tab1, text='Browse', command=getFilePath, font=("Arial", 10), width = 10, bg='#add8e6')
label1.grid(row=0, column=0, padx = 15, pady=10, sticky = W)
entry1.grid(row=1, column=0, padx=15, pady=5)
button1.grid(row=1, column=1, padx=0, pady=0, sticky = W)
gui.mainloop()
It is because you did not set your entry text value in your function. Supposedly after you get your file path, you should assign the file path text value into the entry.
You can do so by inserting this code into your codes.
entry1.insert(0, "Test Value")
Your codes:
def getFilePath():
file_selected = filedialog.askopenfilename(title="Select File")
filePath.set(file_selected)
print(filePath.get())
entry1.insert(0, filePath.get())
You can refer to https://www.geeksforgeeks.org/how-to-set-the-default-text-of-tkinter-entry-widget/ .
Related
I am trying to create Tabs in python tkinter. I would like the Tab's height to size dynamically based on its content. Below is a sample of my code
from tkinter import *
from tkinter import ttk
root = Tk()
root.title('Frames')
root.geometry('500x500')
frame1 = LabelFrame(root)
frame1.pack(padx=5, pady = 5)
notebook1 = ttk.Notebook(frame1)
notebook1.pack(pady= 5)
nbframe1 = Frame(notebook1, bg = 'white')
nbframe2 = Frame(notebook1, bg = 'grey')
nbframe1.pack(fill ='both', expand=1)
nbframe2.pack(fill ='both', expand=1)
notebook1.add(nbframe1, text= 'A Search')
notebook1.add(nbframe2, text= 'B Search')
a_label = Label(nbframe1, text= 'A_Tag', bg = 'white')
a_label.grid(row = 0, column = 0, sticky=W, padx=5)
b_label = Label(nbframe1, text= 'B_Tag', bg = 'white')
b_label.grid(row = 1, column = 0, sticky=W, padx=5)
a_text = StringVar()
a_entry = Entry(nbframe1, textvariable = a_text, width=50)
a_entry.grid(row = 0, column = 1, sticky=W, padx=5)
b_text = StringVar()
b_entry = Entry(nbframe1, textvariable = b_text, width=25)
b_entry.grid(row = 1, column = 1, sticky=W, padx=5)
root.mainloop()
From my code, the Tabs created have same height even though one is empty. This is based on the height of the Tab with content. How can i set it so that each Tab's height is based only on the contents.
You can bind a function to the virtual event <<NotebookTabChanged>> to adapt the height of the notebook to the currently selected tab using the height option of the notebook:
def on_change_tab(event):
tab = event.widget.nametowidget(event.widget.select()) # get selected tab
event.widget.configure(height=tab.winfo_reqheight()) # resize notebook
With your example:
from tkinter import *
from tkinter import ttk
root = Tk()
root.title('Frames')
root.geometry('500x500')
def on_change_tab(event):
tab = event.widget.nametowidget(event.widget.select())
event.widget.configure(height=tab.winfo_reqheight())
frame1 = LabelFrame(root)
frame1.pack(padx=5, pady=5)
notebook1 = ttk.Notebook(frame1)
notebook1.pack(pady=5)
nbframe1 = Frame(notebook1, bg='white')
nbframe2 = Frame(notebook1, bg='grey')
notebook1.add(nbframe1, text='A Search')
notebook1.add(nbframe2, text='B Search')
a_label = Label(nbframe1, text='A_Tag', bg='white')
a_label.grid(row=0, column=0, sticky=W, padx=5)
b_label = Label(nbframe1, text='B_Tag', bg='white')
b_label.grid(row=1, column=0, sticky=W, padx=5)
a_text = StringVar()
a_entry = Entry(nbframe1, textvariable=a_text, width=50)
a_entry.grid(row=0, column=1, sticky=W, padx=5)
b_text = StringVar()
b_entry = Entry(nbframe1, textvariable=b_text, width=25)
b_entry.grid(row=1, column=1, sticky=W, padx=5)
root.update_idletasks() # force display update so that the initial tab is displayed correctly
notebook1.bind('<<NotebookTabChanged>>', on_change_tab) # binding to update the height
root.mainloop()
So I want this Tkinter entry but I can't find it what am I doing wrong?
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)
frame = tk.Frame(root)
main_entry = tk.Entry(root, width = 100, fg = "black").place()
frame.place(relx=.5,rely=.5, anchor='center')
root.mainloop()
I want to make the entry show, why is gone I did everything right?
here, put layout options on variables like this (if You want to just place a widget somewhere use .pack which also does not necessarily require arguments):
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)
frame = tk.Frame(root)
main_entry = tk.Entry(root, width = 100, fg = "black")
main_entry.place(x=50, y=50)
frame.place(relx=.5,rely=.5, anchor='center')
root.mainloop()
The title says it all.
import tkinter as tk
from tkinter import filedialog, Text, StringVar
root = tk.Tk()
root.resizable(False, False)
var1 = StringVar()
topn = ""
print(topn)
def addApp():
global topn
topn = topn + "1"
print(topn)
var1.set(topn)
canvas = tk.Canvas(root, height=500, width=400, bg="#263D42")
canvas.pack()
frame = tk.Frame(root, bg="orange")
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.1)
Resultlabel = tk.Label(root,text=var1.get, padx=100, pady=5, fg="white", bg="grey")
Resultlabel.pack()
t
openFile = tk.Button(frame, text="Open File", padx=10, pady=5, fg="white", bg="#263D42", command=addApp)
openFile.pack()
root.mainloop()
[btw, ignore the Open File, i was following a tutorial to learn the basics of tkinter and then edited the code to fit my needs. I forgot to change the text in the button.]
https://i.stack.imgur.com/2FVKG.png
text is not the same thing as textvariable. text is used to associate static text with a widget, textvariable is used to have the text of the widget be synchronized with the value of a variable.
If you want ResultLabel to always show what is in var1 you must define it like this:
Resultlabel = tk.Label(root,textvariable=var1, padx=100, pady=5, fg="white", bg="grey")
I have this small python tkinter project that basically does two things:
It receives data from the tkinter text widget and enters it into the tkinter Radiobutton widget in another window. Everything is working just fine except one issue.
It treats everything I enter as one and create only one Radiobutton. I want it to create a new Radiobutton anytime I move to a new line
Here is my code:
from tkinter import*
import re
import tkinter.scrolledtext as src
root=Tk()
root.geometry("400x400")
root.title("Still trying hard!!")
opt=StringVar()
db=opt.get()
var=StringVar()
var.get()
Txt_Cont=""
cont_formater=""
#FUNCTIONS
def _Text_Input(a):
if isinstance(db,str):
global Txt_Cont
root1 = Toplevel()
root1.geometry("400x400+500+200")
root1.title("DB AND TB WINDOWS")
cont=Txt_Cont
Radiobtn=Checkbutton(root1,text=cont, variable=var)
Radiobtn.deselect()
Radiobtn.pack()
root1.mainloop()
def btnfcn():
global Txt_Cont
Txt_Cont = text_box.get("1.0", "end-1c")
text_box = src.ScrolledText(root, width=40, height=10, bd=10, font=('arial', 10, 'bold'), padx=5,
pady=5)
text_box.pack()
btn = Button(root, text="click me", font=('arial', 10, 'bold'), command=btnfcn)
btn.pack(ipadx=50, pady=(10, 10))
Options = OptionMenu(root, opt, "Databases", "Tables", command=_Text_Input)
Options.pack(ipadx=60)
root.mainloop()
On create window, tkinter.scrolledtext look up for SearchIP() to insert but never update layout. When I have different input for SearchIP() method never show up. It only shows the initial value.
Note: I added a couple of print statements to watch if SearchIP() returns the right thing, which is the case, for example for 'www.google.com' it prints '216.58.192.4', but this value never shows up in ScrolledText.
import socket
try:
# for Python2
import Tkinter as tk
import ScrolledText as tkst
except ImportError:
# for Python3
import tkinter as tk
import tkinter.scrolledtext as tkst
global Filename
root = tk.Tk()
frame = tk.Frame(root,width=100)
label = Label(root,text="http://")
label.grid(row=0,column=0)
entryVar = tk.StringVar()
entry = Entry(root,width=50,textvariable=entryVar)
entry.grid(row='0',column='1',columnspan=8)
def SearchIP():
print(entryVar.get())
add = str(entryVar.get())
ip_a = socket.gethostbyname(add)
print(str(ip_a))
return str(ip_a);
button1 = Button(root, text="Search IP", command=SearchIP)
button1.grid(row=1, column=0)
button2 = Button(root, text="DNS Recon")
button2.grid(row=1, column=1)
button3 = Button(root, text="Port Scanner")
button3.grid(row=1, column=2)
button4 = Button(root, text="Web Crawl")
button4.grid(row=1, column=3)
button5 = Button(root, text="Email Gathering")
button5.grid(row=1, column=4)
frame.grid(row=2, column=0, columnspan=30, rowspan=30)
edit_space = tkst.ScrolledText(
master = frame,
wrap = 'word', # wrap text at full words only
width = 45, # characters
height = 10, # text lines
# background color of edit area
)
# the padx/pady space will form a frame
edit_space.pack(fill='both', expand=True, padx=8, pady=8)
root.title("E-Z Security Audting")
root.resizable(True, True)
edit_space.insert('insert', SearchIP())
root.mainloop()
I also tried to modify the SearchIP() method and remove the return statement, but then I got this error:
File "C:/Users/kero/Desktop/Robert_HodgesKeroles_Hakeem_secproject/secproject/newUser_Interface.py", line 50, in <module>
edit_space.insert('insert', SearchIP())
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2986, in insert
self.tk.call((self._w, 'insert', index, chars) + args)`
SearchIP after modification
def SearchIP():
add = str(entryVar.get())
ip_a= socket.gethostbyname(add)
You have to use insert() inside SearchIP()
import socket
try:
# for Python2
import Tkinter as tk
import ScrolledText as tkst
except ImportError:
# for Python3
import tkinter as tk
import tkinter.scrolledtext as tkst
# --- functions ---
def SearchIP():
add = entryVar.get()
ip_a = socket.gethostbyname(add)
edit_space.insert('insert', ip_a + "\n")
# --- main ---
root = tk.Tk()
root.title("E-Z Security Audting")
root.resizable(True, True)
frame = tk.Frame(root, width=100)
frame.grid(row=2, column=0, columnspan=30, rowspan=30)
label = tk.Label(root, text="http://")
label.grid(row=0, column=0)
entryVar = tk.StringVar()
entry = tk.Entry(root, width=50, textvariable=entryVar)
entry.grid(row=0, column=1, columnspan=8)
button1 = tk.Button(root, text="Search IP", command=SearchIP)
button1.grid(row=1, column=0)
button2 = tk.Button(root, text="DNS Recon")
button2.grid(row=1, column=1)
button3 = tk.Button(root, text="Port Scanner")
button3.grid(row=1, column=2)
button4 = tk.Button(root, text="Web Crawl")
button4.grid(row=1, column=3)
button5 = tk.Button(root, text="Email Gathering")
button5.grid(row=1, column=4)
edit_space = tkst.ScrolledText(
master=frame,
wrap='word', # wrap text at full words only
width=45, # characters
height=10, # text lines
# background color of edit area
)
edit_space.pack(fill='both', expand=True, padx=8, pady=8)
root.mainloop()