Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm trying to make some basic GUI and I'm having some trouble with this code:
with open(project_dir + 'logs/wash.log') as f:
for line in f.readlines():
if not line.startswith('BSSID ') and \
not line.startswith('------------'):
print(line)
At this point of the application I have the tkinter root already opened in the background and a terminal opened too, I would like to open a new tkinter window in which display the line printed above, I suppose adding a label in the window for each line that I need to display.
I tried tk.Toplevel() but I don't know how to make the new window in which display the strings.
My problem is I am trying to create a new window and print the strings there, I have tried tk.Toplevel() but I don't know how to make the new window in which to display the strings
From your comment:
my problem is to create a new window and print there the strings, I tried tk.Toplevel() but I don't know how to make the new window in which display the strings
This is a simple example but should help.
I have a button on the root window that links to a function called new_window(). This function will create a top level window containing a text box widget. We then use the with open statement to write the data to the text box.
import tkinter as tk
root = tk.Tk()
def new_window():
top = tk.Toplevel(root)
my_text_box = tk.Text(top)
my_text_box.pack()
with open(project_dir + 'logs/wash.log') as f:
for line in f.readlines():
if not line.startswith('BSSID ') and \
not line.startswith('------------'):
my_text_box.insert("end", line)
open_new_window = tk.Button(root, text="Open Toplevel", command=new_window)
open_new_window.pack()
root.mainloop()
Related
This question already has an answer here:
When should I use root.update() in tkInter for python
(1 answer)
Closed 1 year ago.
Beginner programmer here currently trying to learn Tkinter for a school assignment.
I have a GUI class that stores the Tkinter labels etc, the labels are innitiated like this:
# GUI for Player 1
self.player_1_name_field = Label(
self.root,
text="Player 1",
font=GUI_Settings.player_information_font,
anchor=W,
background=GUI_Settings.playerfield_active_color
)
I then create a Game() object that looks like this:
class Game():
def __init__(self):
self.GUI = GUI()
self.GUI.initializeBoard()
self.GUI.root.mainloop()
When I run the code, the labels do get created and are where they are supposed to be, but are completely black. Once I move or resize the window it instantly becomes how I want it to be, it just behaves weird when at the start of the code
The interesting thing is that I also have a Canvas and a List that work perfectly fine, only the Labels are not cooperative
If you need further info, just ask for it!
Thank you!
Edit 1: I have a function called drawWindow() that redraws the chessboard when I re-configure the window. In the init of the GUI class I set self.root.bind("<Configure>", self.drawWindow). If I remove that line of code, the Labels work but the Canvas doesn't anymore. I'm so confused. For anyone wanting to take a look at my tiny code: https://codeshare.io/DZYzyZ
See comment of Thingamabobs
The issue is self.root.update(). Remove this line and you'll be fine.
When should I use root.update() in tkInter for python.
This works but you shouldn't do it
This is a tricky issue. Your problem come from the bind of the configure event. Bind to the root window, it is applied to all sub-widgets of the window, which cause the bug (I don't know why yet).
This will solve your issue (line 202):
self.chessboard.bind("<Configure>", self.drawWindow)
instead of:
self.root.bind("<Configure>", self.drawWindow)
Result without moving or resizing the window:
I found the information here (french forum).
This question already has answers here:
How to handle a click button event in python module tkinter
(2 answers)
Closed 2 years ago.
Here is my code!
///
from tkinter import *
import os
import pygame
os.system(‘clear’)
#Window Setup
root = Tk()
root.title(‘cottontail’)
root.geometry(‘800x600’)
frame = Frame(root)
title_screen = Label(root, text = “Choose your rabbits name!”)
title_screen.pack()
name = Text(root, width=10 , height=3)
name.pack()
confirm_name = Button(root, text= “Conirm?”, width = 5, height=3)
root.mainloop()
///
My objective is to take the input the user puts in the text box to make a label in a pygame window with that name. I figured that a button would be an easy way to confirm the name and open the pygame screen. If this makes any sense to you it would really be appreciated if you could help me. Hope you have a good night!
Thanks in advance!
I'm not familiar with tkinter as I'm a PyQt dev, but I can help with your question as the logic behind both of them are same.
You can make a function that is called by the button whenever its clicked, and then, inside that function definition, do whatever you want.
Here's the website that explains this in detail -
https://www.delftstack.com/howto/python-tkinter/how-to-get-the-input-from-tkinter-text-box/
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is my current db.py file, it contains the tkinter code for creating the GUI:
import tkinter
import db
app = Tk()
app.geometry("450x300")
app.mainloop()
You can use a Entry widget with a linked variable to get the input from user. The content can then be retrieved from the variable and written to a file using file objects.
I like to use themed-tkinter(ttk). If you just starting with creating GUI, I suggest you read more about themed-tkinter here.
from tkinter import ttk
import tkinter as tk
root = tk.Tk()
# StringVar has all the logic to store and update string values
content = tk.StringVar()
def callback():
# This function is invoked when button `submit` is clicked
with open('content.txt', 'w') as file:
file.write(content.get())
entry = ttk.Entry(root, textvariable=content).grid()
submit = ttk.Button(root, text='submit', command=callback).grid()
root.mainloop()
Edit: I worded my answer wrongly for which I appologize. Tkinter by itself is indeed robust and powerful. I found it more easier to use ttk in many cases and had a softcorner towards it.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I am attempting to nest one ttk notebook within another so that I can have multiple tab levels.
Imagine an upper notebook with a tab for each food group, and within each of those foodgroup tabs, a tab for examples of foods in that group. A tabbed hierarchy.
Is it possible with ttk notebooks? I have not been able to find any reference or examples that deal with this question.
It seems like this code should work. I get no errors, but I can't see the second level tabs. Any help would be appreciated.
#import tkinter and ttk modules
from tkinter import *
from tkinter import ttk
#Make the root widget
root = Tk()
#Make the first notebook
nb1 = ttk.Notebook(root)
nb1.pack()
f0 = Frame(nb1)
f0.pack(expand=1, fill='both')
###Make the second notebook
nb2 = ttk.Notebook(f0)
nb2.pack()
#Make 1st tab
f1 = Frame(nb1)
#Add the tab to notebook 1
nb1.add(f1, text="First tab")
#Make 2nd tab
f2 = Frame(nb1)
#Add 2nd tab to notebook 1
nb1.add(f2, text="Second tab")
###Make 3rd tab
f3 = Frame(nb2)
#Add 3rd tab to notebook 2
nb2.add(f3, text="First tab")
###Make 4th tab
f4 = Frame(nb2)
#Add 4th tab to notebook 2
nb2.add(f4, text="Second tab")
root.mainloop()
Solved:
Here is the working code simplified with notation. Hopefully others will find it instructive. This example uses the model of College Program>Terms>Courses
#import tkinter and ttk modules
from tkinter import *
from tkinter import ttk
#Make the root widget
root = Tk()
#Make the first notebook
program = ttk.Notebook(root) #Create the program notebook
program.pack()
#Make the terms frames for the program notebook
for r in range(1,4):
termName = 'Term'+str(r) #concatenate term name(will come from dict)
term = Frame(program) #create frame widget to go in program nb
program.add(term, text=termName)# add the newly created frame widget to the program notebook
nbName=termName+'courses'#concatenate notebook name for each iter
nbName = ttk.Notebook(term)#Create the notebooks to go in each of the terms frames
nbName.pack()#pack the notebook
for a in range (1,6):
courseName = termName+"Course"+str(a)#concatenate coursename(will come from dict)
course = Frame(nbName) #Create a course frame for the newly created term frame for each iter
nbName.add(course, text=courseName)#add the course frame to the new notebook
root.mainloop()
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'd like to get all file names in a directory and present them to user in a listbox, then user may choose multiple names and press OK or Cancel. If he presses OK, it should return selected file names. Please help.
here is a fairly simple way using Tkinter:
from Tkinter import *
root = Tk()
opt_list = ['opt1','opt2','opt3','opt4','opt5']
sel_list = []
def get_sel():
sel_list.append(Lb1.curselection())
root.destroy()
def cancel():
root.destroy()
B = Button(root, text ="Submit", command = get_sel)
C = Button(root, text ="Cancel", command = cancel)
Lb1 = Listbox(root, selectmode=MULTIPLE)
for i,j in enumerate(opt_list):
Lb1.insert(i,j)
Lb1.pack()
B.pack()
C.pack()
root.mainloop()
for i in sel_list[0]:
print opt_list[int(i)]
then you can this to get the selected options:
for i in sel_list[0]:
print opt_list[int(i)]
this will create a listbox using the items from sel_list then when the user presses submit it will return a tuple of which lines are selected
multiple can be selected at a time and will returned in a tuple get more information from this site Python Tk Tutorials Point
More specifically, what you want is http://tkinter.unpythonic.net/wiki/tkFileDialog
#python 3
from tkinter.filedialog import askopenfilename
filenames = askopenfilename(multiple=True)
This returns a list of paths to the files the person chose, to extract the filename:
import os
filenames = [os.path.basename(filename) for filename in filenames]
and if you want the filename without extension, instead of the line above use:
filenames = [os.path.splitext(os.path.basename(filename))[0] for filename in filenames]