I want to make popup window using Tkinter.
I can do it so:
import Tkinter
a="some data that use should be able to copy-paste"
tkMessageBox.showwarning("done","message")
But there is one problem that user need to be able to select, copy and paste shown text.
It's not possible to do in such way.
Are there any ways to do it with Tkinter? (or another tools that is supplied with python by default)
Thanks in advance for any tips
From here, it seems a workaround using Entry in Tkinter is doable. Here is the code:
import Tkinter as Tk
root = Tk.Tk()
ent = Tk.Entry(root, state='readonly')
var = Tk.StringVar()
var.set('Some text')
ent.config(textvariable=var, relief='flat')
ent.pack()
root.mainloop()
EDIT: To respond to your comment, I found a way to insert multi-line text, using the Text widget.
Here is a draft of a solution:
from Tkinter import *
root = Tk()
T = Text(root, height=2, width=30, bg='lightgrey', relief='flat')
T.insert(END, "Just a text Widget\nin two lines\n")
T.config(state=DISABLED) # forbid text edition
T.pack()
mainloop()
I'm (still) interested in any better solution :)
You can use buttons for copy and paste. First you need to select. In a text widget it is easily done by
selection=nameoftextwidget.get(SEL_FIRST,SEL_LAST)
Then you can use this for copying easily by the use of selection. If you want to copy/paste it in that same text widget, you can use:
nameoftextwidget.insert(END,"\n"+selection)
Related
By placing a Text widget in my GUI
from Tkinter import Text
textBox=Text(root, height=20, width=10)
textBox.pack()
whenever I write something in that box, I cant return the focus back to any other place in the window. I have some keys bounded to event, which stop working after I wrote in the Text widget.
Is there a way of redirecting the focus to another place after writing text?
Please press Return-Key to give focus back to window
import tkinter as tk
def onReturn(*event):
root.focus_set()
root = tk.Tk()
textBox= tk.Text(root, height=20, width=10)
textBox.pack()
root.bind("<Return>", onReturn)
root.mainloop()
Is there a way of redirecting the focus to another place after writing text?
Every widget has a method named focus_set which can be used to move keyboard focus to that widget.
For example, to set the focus to the root window you would do:
root.focus_set()
I want to create a hyperlink in Tkinter. Here is my approach:
from tkinter import *
import webbrowser
def callback(event):
webbrowser.open_new(r"http://www.google.com")
root = Tk()
link = Label(root, text="Google Hyperlink", fg="blue", cursor="hand2")
link.pack()
link.bind("<Button-1>", callback)
root.mainloop()
I have a piece of text, a few sentences, and only a few words in it should be a hyperlink. How can I do that? I need a simple solution.
You have to use Text (or Canvas) widget. You can make them look like label, so that users won;t tell the difference.
Create a tag within your Text (see example here Set to bold the selected text using tags) and bind the tag to the callback.
As suggested by others, using a Text widget is the best way to do this.
One potential solution to this problem is provided here
I'm trying to prepopulate a text field based on the most recent entry. As this is not a Listbox, I don't see how to do it, and I'm not seeing any examples on the web. Thanks.
Update. I've managed to find a partial way of doing this. Still wondering, is it possible to supply suggested text in Tkinter which fades when the text box is clicked?
from Tkinter import *
app = Tk()
app.title("GUI Example")
app.geometry('560x460+200+200')
x = Text(app)
x.insert(END, "Before")
x.pack()
def replace():
x.delete(1.0, END)
x.insert(END, "After")
abutton = Button(app, text="Click me", command=replace)
abutton.pack()
app.mainloop()
Well, I personally don't know of any options to do this (any answers giving one will easily trump this one).
However, you can closely mimic this behavior with a little coding. Namely, you can bind the textbox to a function that will insert/remove the default text for you.
Below is a simple script to demonstrate:
import Tkinter as tk
tk.Tk()
textbox = tk.Text(height=10, width=10)
textbox.insert(tk.END, "Default")
textbox.pack()
# This is for demonstration purposes
tk.Text(height=10, width=10).pack()
def default(event):
current = textbox.get("1.0", tk.END)
if current == "Default\n":
textbox.delete("1.0", tk.END)
elif current == "\n":
textbox.insert("1.0", "Default")
textbox.bind("<FocusIn>", default)
textbox.bind("<FocusOut>", default)
tk.mainloop()
Notice how:
When you click in the top textbox, the default text disappears.
When you click in the bottom textbox, the top one loses focus and the default text reappears.
This behavior will only occur if there is nothing in the top textbox.
I am wondering if it is possible to redirect the output of a function to a tab in the ttk notebook widget.
I assumed it would be similar to the listbox widget where you just used listbox.insert but I can not get this to work.
I apologize if this is a simple question but its really stumped me and I am unable to find any helpful material online to help me.
thanks in advance
Im using python 3.3
The Insert function works almost the same, the only difference is instead of taking a string it takes a frame. Add is a lot simpler though, you don't need to specify an index it just adds it to the end. All you need to do is create a frame, pack a Text element into it, and then pack the whole thing into the notebook. It would look something like this
noteb = ttk.Notebook( root, width=500, height=300 )
frame1 = tkinter.Frame( noteb )
textbox = tkinter.Text( frame1, put whatever you want to put here )
frame1.pack( expand=1, fill='both' )
noteb.add( frame1, whatever parameters you want )
noteb.pack( expand=1, fill='both' )
You should then be able to change the text in textbox directly.
Depends somewhat on what type of widgets you use as tabs, but basically it shouldn't be very different. Just keep track of your tab widgets and call the appropriate method.
Example with two Text tabs:
from Tkinter import *
from ttk import Notebook
def addText(tab):
tab.insert(END, "foo! ")
root = Tk()
nb = Notebook(root, height=240, width=480)
tabs = {"foo": [], "bar": []}
for tabname in tabs:
tab = Text(nb)
tabs[tabname] = tab
nb.add(tab, text= tabname)
nb.pack()
Button(root, text= "Add text!", command = lambda: addText(tabs["foo"])).pack()
root.mainloop()
Clicking the "Add text!" button appends some text to the first tab.
from Tkinter import *
root = Tk()
root.title("Whois Tool")
text = Text()
text1 = Text()
text1.config(width=15, height=1)
text1.pack()
def button1():
text.insert(END, text1)
b = Button(root, text="Enter", width=10, height=2, command=button1)
b.pack()
scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
text.config(width=60, height=15)
text.pack(side=LEFT, fill=Y)
scrollbar.config(command=text.yview)
text.config(yscrollcommand=scrollbar.set)
root.mainloop()
How can I add the data from a text widget to another text widget?
For example, I'm trying to insert the data in text1 to text, but it is not working.
You are trying to insert a Text reference at the end of another Text widget (does not make much sense), but what you actually want to do is to copy the contents of a Text widget to another:
def button1():
text.insert(INSERT, text1.get("1.0", "end-1c"))
Not an intuitive way to do it in my opinion. "1.0" means line 1, column 0. Yes, the lines are 1-indexed and the columns are 0-indexed.
Note that you may not want to import the entire Tkinter package, using from Tkinter import *. It will likely lead to confusion down the road. I would recommend using:
import Tkinter
text = Tkinter.Text()
Another option is:
import Tkinter as tk
text = tk.Text()
You can choose a short name (like "tk") of your choice. Regardless, you should stick to one import mechanism for the library.