5

I'm trying to write a Python program that gets a string as input and displays the string in a message box with the last letter removed from each word. I've successfully written the code to remove the last letter from each word and I came to know about the tkinter module. But the text isn't copy-able from tk message box. Is there any other way to display a message box with copy-able text? If there's no way to display such message boxes, is there any way to display the output in a copy-able form without displaying a message box? Additional (useless) information:

  1. The name of this language is fromonk(In case you were wondering why the var name fromonk_text)
  2. Smileys should be displayed in whole.(Including the last letter).Hence the if-else block.

The code I've written:

import tkMessageBox
line="foo"
while line!="exit":
    fromonk_text=""
    line=raw_input()
    words=line.split()
    for word in words:
        if word.startswith(":"):
            fromonk_text+=word+" "
        else:
            fromonk_text+=word[0:len(word)-1]+" "
    tkMessageBox.showinfo("Fromonk",fromonk_text)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

1 Answers1

4

There is nothing built-in. You can create your own popup dialog with a Toplevel widget that contains a Text widget and some Button widgets, or you can use tkSimpleDialog

Some documentation can be found here: http://effbot.org/tkinterbook/tkinter-dialog-windows.htm

Here's a simple working example. It allows the text to be edited, but you can disable that if you want.

import Tkinter as tk
import tkSimpleDialog

class CustomDialog(tkSimpleDialog.Dialog):

    def __init__(self, parent, title=None, text=None):
        self.data = text
        tkSimpleDialog.Dialog.__init__(self, parent, title=title)

    def body(self, parent):

        self.text = tk.Text(self, width=40, height=4)
        self.text.pack(fill="both", expand=True)

        self.text.insert("1.0", self.data)

        return self.text

def show_dialog():
    fromonk_text = "this is an example"
    CustomDialog(root, title="Example", text=fromonk_text)

root = tk.Tk()
button = tk.Button(root, text="Click me", command=show_dialog)
button.pack(padx=20, pady=20)
root.mainloop()

Example Window

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 3
    [For Python 3 users](https://stackoverflow.com/a/673309/3357935), rename the following imports: `Tkinter` → `tkinter`, `tkSimpleDialog` → `tkinter.simpledialog` – Stevoisiak Mar 01 '18 at 21:07
  • This answer serves as a nice example of how to inherit / override the `tkSimpleDialog` class if that's the right way of wording it. I'm interested because this class has `self.wait_window(self)` which hopefully allows tkinter run in background until user clicks button. – WinEunuuchs2Unix Nov 01 '20 at 15:38
  • @WinEunuuchs2Unix: tkinter doesn't "run in the background". Tkinter is single-threaded. What `wait_window` does is allow `mainloop` to continue to process events, but function won't return until the dialog is destroyed. – Bryan Oakley Nov 01 '20 at 17:21
  • @BryanOakley I'm not sure what `mainloop` is supposed to do but I can say that all `tkinter` windows freeze when `wait_window` runs in `tkSimpleDialog` so i will have to make a special dialog box background process like all the other windows I've had to create so that all `tkinter` windows run concurrently. I'll do that later though because I've so much more code to write. Thank you for your kind patience explaining things to me so often. – WinEunuuchs2Unix Nov 03 '20 at 02:04
  • @WinEunuuchs2Unix: I've never seen all windows freeze when calling `wait_window`. In fact, in a sense that function was created specifically so that the windows would _not_ freeze. All `mainloop` does it continuously process events from the event queue. It's barely more than a `while` loop, and `wait_window` is barely more than `mainloop` with an extra check to see if the window exists before returning. – Bryan Oakley Nov 03 '20 at 05:02