Posts: 1,091
Threads: 143
Joined: Jul 2017
I'm trying to learn tkinter.
In my window I have 2 frames, frame1 and frame2. Everything displays OK.
I can get a text entry in frame1 and I can echo it in frame1 by clicking this button.
button1 = tkinter.Button(text='Get the text message', command=getInput, bg='brown', fg='white', font=('helvetica', 9, 'bold'))
canvas1.create_window(200, 110, window=button1) frame2 contains a scrolled text widget.
I would like to echo the content of entry1 from frame1 in the text widget of frame2.
How do I do that?
entry1 = tkinter.Entry (frame1)
canvas1.create_window(200, 70, window=entry1)
def getInput():
global the_id
canvas1.delete(the_id)
global msg
msg = entry1.get()
label3 = tkinter.Label(frame1, text= 'You entered this text:',font=('helvetica', 10))
canvas1.create_window(200, 140, window=label3)
# wraplength is in pixels not characters!!
label4 = tkinter.Label(frame1, text=msg,font=('helvetica', 10, 'bold'), anchor='w', wraplength=390)
the_id = canvas1.create_window(200, 170, window=label4, tags="label") I set msg as global Quote:global msg
and I tried this below. I do not get any errors, but neither do I get any output in frame2:
text = scrolledtext.ScrolledText(frame2, width=40, height=10, bd=5, undo=True)
text['font'] = ('consolas', '12')
msg = entry1.get()
text.insert('1.end', msg + '\n')
#txt.pack(expand=True, fill='both')
text.pack(expand=False) Seems like I can't reach from frame2 to a variable in frame1. How to shunt it over?
Posts: 12,027
Threads: 485
Joined: Sep 2016
Make it easier on us, please submit full code.
Posts: 1,091
Threads: 143
Joined: Jul 2017
Full code:
def myApp():
window = tkinter.Tk()
window.title("Get a text input and echo it")
window.config(bg='light blue')
window.geometry('640x640')
global msg
frame1=Frame(window,width=500,height=400, bg='green')
#frame.pack(expand=True, fill=BOTH) #.grid(row=0,column=0)
frame1.pack(expand=False)
frame2=Frame(window,width=500,height=400, bg='green')
#frame.pack(expand=True, fill=BOTH) #.grid(row=0,column=0)
frame2.pack(expand=False)
canvas1 = tkinter.Canvas(frame1, bg='white', width = 400, height = 200, relief = 'raised')
#canvas1.config(xscrollcommand=xscrollbar.set, yscrollcommand=yscrollbar.set)
canvas1.pack(padx=10, pady=10)
label1 = tkinter.Label(canvas1, text='Echo a text input')
label1.config(font=('helvetica', 14))
canvas1.create_window(200, 10, window=label1)
label2 = tkinter.Label(frame1, text='Type your text:')
label2.config(font=('helvetica', 10))
canvas1.create_window(200, 40, window=label2)
entry1 = tkinter.Entry (frame1)
canvas1.create_window(200, 70, window=entry1)
def getInput():
global the_id
canvas1.delete(the_id)
global msg
msg = entry1.get()
label3 = tkinter.Label(frame1, text= 'You entered this text:',font=('helvetica', 10))
canvas1.create_window(200, 140, window=label3)
# wraplength is in pixels not characters!!
label4 = tkinter.Label(frame1, text=msg,font=('helvetica', 10, 'bold'), anchor='w', wraplength=390)
the_id = canvas1.create_window(200, 170, window=label4, tags="label")
# don't set the y value too high or you won't see the output!
#canvas1.create_window(200, 320, window=label4)
global the_id
#global the_id
label4 = tkinter.Label(frame1, text= 'Your input will be here after you click the button.',font=('helvetica', 10, 'bold'), anchor='w', wraplength=390)
#the_id = tkinter.Label(frame, text= 'Your input will be here after you click the button.',font=('helvetica', 10, 'bold'), anchor='w', wraplength=390)
the_id = canvas1.create_window(200, 170, window=label4, tags="label")
button1 = tkinter.Button(text='Get the text message', command=getInput, bg='brown', fg='white', font=('helvetica', 9, 'bold'))
canvas1.create_window(200, 110, window=button1)
text = scrolledtext.ScrolledText(frame2, width=40, height=10, bd=5, undo=True)
text['font'] = ('consolas', '12')
global msg
msg = entry1.get()
text.insert('1.end', msg + '\n')
#txt.pack(expand=True, fill='both')
text.pack(expand=False)
window.mainloop()
Posts: 12,027
Threads: 485
Joined: Sep 2016
This code is riddled with errors.
you shouldn't use globals, there's no need for them.
with tkinter, you should use classes, forward referencing will no longer be a problem.
Here's a basic tkinter application that's done with classes: https://python-forum.io/Thread-GUI-list-...ht=tkinter
Posts: 1,091
Threads: 143
Joined: Jul 2017
Jul-09-2020, 10:13 PM
(This post was last modified: Jul-09-2020, 10:14 PM by Pedroski55.)
Thanks, trying your code, but these 2 both fail:
pip3 install tkinter.tix
pip3 install tkinter.filedialog
This is the error I get in Idle:
Quote:Traceback (most recent call last):
File "<pyshell#11>", line 2, in <module>
root = tk.Tk()
File "/usr/lib/python3.6/tkinter/tix.py", line 214, in __init__
self.tk.eval('package require Tix')
_tkinter.TclError: can't find package Tix
pip3 install Tix also fails
Posts: 6,780
Threads: 20
Joined: Feb 2020
Not sure what problem you are having with tkinter.filedialog or tkinter.tix. Check for a spelling or another case error. Both are part of the standard libraries and you should not have to install them. What platform are you using? What version of Python?
Posts: 1,091
Threads: 143
Joined: Jul 2017
Jul-10-2020, 10:09 PM
(This post was last modified: Jul-10-2020, 11:36 PM by Pedroski55.)
I use Ubuntu 18.04
You are right, in that
import tkinter.tix as tk
import tkinter.filedialog as tf
import pip Do not show any errors.
But when I try and run Larz60+ code in Idle, I still get:
Quote:Traceback (most recent call last):
File "<pyshell#5>", line 2, in <module>
root = tk.Tk()
File "/usr/lib/python3.6/tkinter/tix.py", line 214, in __init__
self.tk.eval('package require Tix')
_tkinter.TclError: can't find package Tix
What is Tix?
I finally found the tkinter docs. I will buy the book and work through them. Maybe I won't have so many problems after that! (said he, hopefully but uncertain)
Posts: 1,144
Threads: 114
Joined: Sep 2019
Is this what you are trying to do?
#! /usr/bin/env python3
# Do the imports
import tkinter as tk
from functools import partial
root = tk.Tk()
root['padx'] = 20
root['pady'] = 10
def printit(text):
text_area.delete(1.0, tk.END)
text_area.insert(tk.END, text.get())
frame = tk.Frame(root)
frame.grid(column=0, row=0, sticky='nw', padx=10)
frame2 = tk.Frame(root)
frame2.grid(column=1, row=0, sticky='nw', padx=10)
entry = tk.Entry(frame, width=40)
entry.focus()
entry.grid(column=0, row=0, sticky='n')
text_area = tk.Text(frame2, width=40, height=10)
text_area.grid(column=0, row=0)
btn = tk.Button(root, text='Submit', command=partial(printit, entry))
btn.grid(columnspan=2, column=0, row=2, sticky='w')
root.mainloop()
Posts: 1,091
Threads: 143
Joined: Jul 2017
Thank you very much, that's great! Worked first time!
html is just a text file. Python is great for dealing with text files.
What I eventually want to do is put all my little 'makeHTML' programs in a GUI.
I have about 16 little programs for creating html, inserting media files, checkboxes, textboxes, dropdownboxes, I run them in bash. Each one requires some input from me and each one pumps out some text, so I can see what it's doing. They all work well and do just what I want.
So, eventually, I will have a GUI with 16 buttons, or a menu with 16 items, click one and off it goes. They all append their output to the same file.
This is all just for homework webpages. I also use Python to make the php file that gets the students' answers and writes them to my webpage. Python really saves me a lot of time!
|