Hướng dẫn how do you input a text box in python? - làm thế nào để bạn nhập một hộp văn bản trong python?

Thư viện TK thực sự có một chức năng thực hiện điều này mặc dù nó là 'riêng tư'. Bạn có thể sử dụng nó như sau.

import tkinter as tk

root = tk.Tk[]
root.wm_geometry["800x600"]
dialog = tk.Toplevel[root]
root_name = root.winfo_pathname[root.winfo_id[]]
dialog_name = dialog.winfo_pathname[dialog.winfo_id[]]
root.tk.eval['tk::PlaceWindow {0} widget {1}'.format[dialog_name, root_name]]
root.mainloop[]

Điều này sẽ đặt hộp thoại của bạn tập trung vào cửa sổ được chỉ định [trong trường hợp này là cửa sổ gốc]. Tham khảo.

Trong một ứng dụng thực tế

Đây là phiên bản tôi đã tạo bởi vì ngoài đời thực, bạn sẽ cần hiển thị một hộp input trên đầu cửa sổ/biểu mẫu chính và thường ở trạng thái phương thức [không thể nhấp vào các cửa sổ khác], sau đó đóng nó khi người dùng Nhấp vào OK:

try:
    # for Python2
    import Tkinter as tk
except ImportError:
    # for Python3
    import tkinter as tk

class App:
    def __init__[self]:
        self.HEIGHT = 700
        self.WIDTH = 800    
        root = tk.Tk[]
        root.width = self.WIDTH
        root.height = self.HEIGHT
        self.dialogroot = root
        self.strDialogResult = ""    
        self.canvas = tk.Canvas[root, height=self.HEIGHT, width=self.WIDTH]
        self.canvas.pack[]    
        frame = tk.Frame[root, bg='#42c2f4']
        frame.place[relx=0.5, rely=0.02, relwidth=0.96, relheight=0.95, anchor='n']  
        # Here is the button call to the InputBox[] function
        buttonInputBox = tk.Button[frame, text="Input Box", bg='#cccccc', font=60, 
        command=lambda: self.InputBox[]]    
        buttonInputBox.place[relx=0.05, rely=0.1, relwidth=0.90, relheight=0.8]    
        root.mainloop[]

    def InputBox[self]:        
        dialog = tk.Toplevel[self.dialogroot]
        dialog.width = 600
        dialog.height = 100

        frame = tk.Frame[dialog,  bg='#42c2f4', bd=5]
        frame.place[relwidth=1, relheight=1]

        entry = tk.Entry[frame, font=40]
        entry.place[relwidth=0.65, rely=0.02, relheight=0.96]
        entry.focus_set[]

        submit = tk.Button[frame, text='OK', font=16, command=lambda: self.DialogResult[entry.get[]]]
        submit.place[relx=0.7, rely=0.02, relheight=0.96, relwidth=0.3]

        root_name = self.dialogroot.winfo_pathname[self.dialogroot.winfo_id[]]
        dialog_name = dialog.winfo_pathname[dialog.winfo_id[]]

        # These two lines show a modal dialog
        self.dialogroot.tk.eval['tk::PlaceWindow {0} widget {1}'.format[dialog_name, root_name]]
        self.dialogroot.mainloop[]

        #This line destroys the modal dialog after the user exits/accepts it
        dialog.destroy[]

        #Print and return the inputbox result
        print[self.strDialogResult]
        return self.strDialogResult

    def DialogResult[self, result]:
        self.strDialogResult = result
        #This line quits from the MODAL STATE but doesn't close or destroy the modal dialog
        self.dialogroot.quit[]


# Launch ...
if __name__ == '__main__':
    app = App[]

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luậnget[start, [end]]

    where,

    Tiện ích hộp văn bản Tkinter được sử dụng để chèn văn bản nhiều dòng. Tiện ích này có thể được sử dụng để nhắn tin, hiển thị thông tin và nhiều tác vụ khác. Nhiệm vụ quan trọng là có được văn bản chèn để xử lý thêm. Đối với điều này, chúng tôi phải sử dụng phương thức get [] cho tiện ích hộp văn bản.

    Cú pháp: Nhận [Bắt đầu, [Kết thúc]]

    Bắt đầu là chỉ mục bắt đầu của văn bản cần thiết trong hộp văn bản

    Approach:

    • Tạo cửa sổ Tkinter
    • Tạo tiện ích hộp văn bản
    • Tạo một tiện ích nút
    • Tạo một hàm sẽ trả về văn bản từ hộp văn bản bằng phương thức get [] sau khi nhấn nút

    Dưới đây là việc thực hiện:

    Python3

    import tkinter as tk

    frame = tk.Tk[]

    frame.title["TextBox Input"]

    try:
        # for Python2
        import Tkinter as tk
    except ImportError:
        # for Python3
        import tkinter as tk
    
    class App:
        def __init__[self]:
            self.HEIGHT = 700
            self.WIDTH = 800    
            root = tk.Tk[]
            root.width = self.WIDTH
            root.height = self.HEIGHT
            self.dialogroot = root
            self.strDialogResult = ""    
            self.canvas = tk.Canvas[root, height=self.HEIGHT, width=self.WIDTH]
            self.canvas.pack[]    
            frame = tk.Frame[root, bg='#42c2f4']
            frame.place[relx=0.5, rely=0.02, relwidth=0.96, relheight=0.95, anchor='n']  
            # Here is the button call to the InputBox[] function
            buttonInputBox = tk.Button[frame, text="Input Box", bg='#cccccc', font=60, 
            command=lambda: self.InputBox[]]    
            buttonInputBox.place[relx=0.05, rely=0.1, relwidth=0.90, relheight=0.8]    
            root.mainloop[]
    
        def InputBox[self]:        
            dialog = tk.Toplevel[self.dialogroot]
            dialog.width = 600
            dialog.height = 100
    
            frame = tk.Frame[dialog,  bg='#42c2f4', bd=5]
            frame.place[relwidth=1, relheight=1]
    
            entry = tk.Entry[frame, font=40]
            entry.place[relwidth=0.65, rely=0.02, relheight=0.96]
            entry.focus_set[]
    
            submit = tk.Button[frame, text='OK', font=16, command=lambda: self.DialogResult[entry.get[]]]
            submit.place[relx=0.7, rely=0.02, relheight=0.96, relwidth=0.3]
    
            root_name = self.dialogroot.winfo_pathname[self.dialogroot.winfo_id[]]
            dialog_name = dialog.winfo_pathname[dialog.winfo_id[]]
    
            # These two lines show a modal dialog
            self.dialogroot.tk.eval['tk::PlaceWindow {0} widget {1}'.format[dialog_name, root_name]]
            self.dialogroot.mainloop[]
    
            #This line destroys the modal dialog after the user exits/accepts it
            dialog.destroy[]
    
            #Print and return the inputbox result
            print[self.strDialogResult]
            return self.strDialogResult
    
        def DialogResult[self, result]:
            self.strDialogResult = result
            #This line quits from the MODAL STATE but doesn't close or destroy the modal dialog
            self.dialogroot.quit[]
    
    
    # Launch ...
    if __name__ == '__main__':
        app = App[]
    
    0
    try:
        # for Python2
        import Tkinter as tk
    except ImportError:
        # for Python3
        import tkinter as tk
    
    class App:
        def __init__[self]:
            self.HEIGHT = 700
            self.WIDTH = 800    
            root = tk.Tk[]
            root.width = self.WIDTH
            root.height = self.HEIGHT
            self.dialogroot = root
            self.strDialogResult = ""    
            self.canvas = tk.Canvas[root, height=self.HEIGHT, width=self.WIDTH]
            self.canvas.pack[]    
            frame = tk.Frame[root, bg='#42c2f4']
            frame.place[relx=0.5, rely=0.02, relwidth=0.96, relheight=0.95, anchor='n']  
            # Here is the button call to the InputBox[] function
            buttonInputBox = tk.Button[frame, text="Input Box", bg='#cccccc', font=60, 
            command=lambda: self.InputBox[]]    
            buttonInputBox.place[relx=0.05, rely=0.1, relwidth=0.90, relheight=0.8]    
            root.mainloop[]
    
        def InputBox[self]:        
            dialog = tk.Toplevel[self.dialogroot]
            dialog.width = 600
            dialog.height = 100
    
            frame = tk.Frame[dialog,  bg='#42c2f4', bd=5]
            frame.place[relwidth=1, relheight=1]
    
            entry = tk.Entry[frame, font=40]
            entry.place[relwidth=0.65, rely=0.02, relheight=0.96]
            entry.focus_set[]
    
            submit = tk.Button[frame, text='OK', font=16, command=lambda: self.DialogResult[entry.get[]]]
            submit.place[relx=0.7, rely=0.02, relheight=0.96, relwidth=0.3]
    
            root_name = self.dialogroot.winfo_pathname[self.dialogroot.winfo_id[]]
            dialog_name = dialog.winfo_pathname[dialog.winfo_id[]]
    
            # These two lines show a modal dialog
            self.dialogroot.tk.eval['tk::PlaceWindow {0} widget {1}'.format[dialog_name, root_name]]
            self.dialogroot.mainloop[]
    
            #This line destroys the modal dialog after the user exits/accepts it
            dialog.destroy[]
    
            #Print and return the inputbox result
            print[self.strDialogResult]
            return self.strDialogResult
    
        def DialogResult[self, result]:
            self.strDialogResult = result
            #This line quits from the MODAL STATE but doesn't close or destroy the modal dialog
            self.dialogroot.quit[]
    
    
    # Launch ...
    if __name__ == '__main__':
        app = App[]
    
    1]

    try:
        # for Python2
        import Tkinter as tk
    except ImportError:
        # for Python3
        import tkinter as tk
    
    class App:
        def __init__[self]:
            self.HEIGHT = 700
            self.WIDTH = 800    
            root = tk.Tk[]
            root.width = self.WIDTH
            root.height = self.HEIGHT
            self.dialogroot = root
            self.strDialogResult = ""    
            self.canvas = tk.Canvas[root, height=self.HEIGHT, width=self.WIDTH]
            self.canvas.pack[]    
            frame = tk.Frame[root, bg='#42c2f4']
            frame.place[relx=0.5, rely=0.02, relwidth=0.96, relheight=0.95, anchor='n']  
            # Here is the button call to the InputBox[] function
            buttonInputBox = tk.Button[frame, text="Input Box", bg='#cccccc', font=60, 
            command=lambda: self.InputBox[]]    
            buttonInputBox.place[relx=0.05, rely=0.1, relwidth=0.90, relheight=0.8]    
            root.mainloop[]
    
        def InputBox[self]:        
            dialog = tk.Toplevel[self.dialogroot]
            dialog.width = 600
            dialog.height = 100
    
            frame = tk.Frame[dialog,  bg='#42c2f4', bd=5]
            frame.place[relwidth=1, relheight=1]
    
            entry = tk.Entry[frame, font=40]
            entry.place[relwidth=0.65, rely=0.02, relheight=0.96]
            entry.focus_set[]
    
            submit = tk.Button[frame, text='OK', font=16, command=lambda: self.DialogResult[entry.get[]]]
            submit.place[relx=0.7, rely=0.02, relheight=0.96, relwidth=0.3]
    
            root_name = self.dialogroot.winfo_pathname[self.dialogroot.winfo_id[]]
            dialog_name = dialog.winfo_pathname[dialog.winfo_id[]]
    
            # These two lines show a modal dialog
            self.dialogroot.tk.eval['tk::PlaceWindow {0} widget {1}'.format[dialog_name, root_name]]
            self.dialogroot.mainloop[]
    
            #This line destroys the modal dialog after the user exits/accepts it
            dialog.destroy[]
    
            #Print and return the inputbox result
            print[self.strDialogResult]
            return self.strDialogResult
    
        def DialogResult[self, result]:
            self.strDialogResult = result
            #This line quits from the MODAL STATE but doesn't close or destroy the modal dialog
            self.dialogroot.quit[]
    
    
    # Launch ...
    if __name__ == '__main__':
        app = App[]
    
    3
    try:
        # for Python2
        import Tkinter as tk
    except ImportError:
        # for Python3
        import tkinter as tk
    
    class App:
        def __init__[self]:
            self.HEIGHT = 700
            self.WIDTH = 800    
            root = tk.Tk[]
            root.width = self.WIDTH
            root.height = self.HEIGHT
            self.dialogroot = root
            self.strDialogResult = ""    
            self.canvas = tk.Canvas[root, height=self.HEIGHT, width=self.WIDTH]
            self.canvas.pack[]    
            frame = tk.Frame[root, bg='#42c2f4']
            frame.place[relx=0.5, rely=0.02, relwidth=0.96, relheight=0.95, anchor='n']  
            # Here is the button call to the InputBox[] function
            buttonInputBox = tk.Button[frame, text="Input Box", bg='#cccccc', font=60, 
            command=lambda: self.InputBox[]]    
            buttonInputBox.place[relx=0.05, rely=0.1, relwidth=0.90, relheight=0.8]    
            root.mainloop[]
    
        def InputBox[self]:        
            dialog = tk.Toplevel[self.dialogroot]
            dialog.width = 600
            dialog.height = 100
    
            frame = tk.Frame[dialog,  bg='#42c2f4', bd=5]
            frame.place[relwidth=1, relheight=1]
    
            entry = tk.Entry[frame, font=40]
            entry.place[relwidth=0.65, rely=0.02, relheight=0.96]
            entry.focus_set[]
    
            submit = tk.Button[frame, text='OK', font=16, command=lambda: self.DialogResult[entry.get[]]]
            submit.place[relx=0.7, rely=0.02, relheight=0.96, relwidth=0.3]
    
            root_name = self.dialogroot.winfo_pathname[self.dialogroot.winfo_id[]]
            dialog_name = dialog.winfo_pathname[dialog.winfo_id[]]
    
            # These two lines show a modal dialog
            self.dialogroot.tk.eval['tk::PlaceWindow {0} widget {1}'.format[dialog_name, root_name]]
            self.dialogroot.mainloop[]
    
            #This line destroys the modal dialog after the user exits/accepts it
            dialog.destroy[]
    
            #Print and return the inputbox result
            print[self.strDialogResult]
            return self.strDialogResult
    
        def DialogResult[self, result]:
            self.strDialogResult = result
            #This line quits from the MODAL STATE but doesn't close or destroy the modal dialog
            self.dialogroot.quit[]
    
    
    # Launch ...
    if __name__ == '__main__':
        app = App[]
    
    4

    Các

    try:
        # for Python2
        import Tkinter as tk
    except ImportError:
        # for Python3
        import tkinter as tk
    
    class App:
        def __init__[self]:
            self.HEIGHT = 700
            self.WIDTH = 800    
            root = tk.Tk[]
            root.width = self.WIDTH
            root.height = self.HEIGHT
            self.dialogroot = root
            self.strDialogResult = ""    
            self.canvas = tk.Canvas[root, height=self.HEIGHT, width=self.WIDTH]
            self.canvas.pack[]    
            frame = tk.Frame[root, bg='#42c2f4']
            frame.place[relx=0.5, rely=0.02, relwidth=0.96, relheight=0.95, anchor='n']  
            # Here is the button call to the InputBox[] function
            buttonInputBox = tk.Button[frame, text="Input Box", bg='#cccccc', font=60, 
            command=lambda: self.InputBox[]]    
            buttonInputBox.place[relx=0.05, rely=0.1, relwidth=0.90, relheight=0.8]    
            root.mainloop[]
    
        def InputBox[self]:        
            dialog = tk.Toplevel[self.dialogroot]
            dialog.width = 600
            dialog.height = 100
    
            frame = tk.Frame[dialog,  bg='#42c2f4', bd=5]
            frame.place[relwidth=1, relheight=1]
    
            entry = tk.Entry[frame, font=40]
            entry.place[relwidth=0.65, rely=0.02, relheight=0.96]
            entry.focus_set[]
    
            submit = tk.Button[frame, text='OK', font=16, command=lambda: self.DialogResult[entry.get[]]]
            submit.place[relx=0.7, rely=0.02, relheight=0.96, relwidth=0.3]
    
            root_name = self.dialogroot.winfo_pathname[self.dialogroot.winfo_id[]]
            dialog_name = dialog.winfo_pathname[dialog.winfo_id[]]
    
            # These two lines show a modal dialog
            self.dialogroot.tk.eval['tk::PlaceWindow {0} widget {1}'.format[dialog_name, root_name]]
            self.dialogroot.mainloop[]
    
            #This line destroys the modal dialog after the user exits/accepts it
            dialog.destroy[]
    
            #Print and return the inputbox result
            print[self.strDialogResult]
            return self.strDialogResult
    
        def DialogResult[self, result]:
            self.strDialogResult = result
            #This line quits from the MODAL STATE but doesn't close or destroy the modal dialog
            self.dialogroot.quit[]
    
    
    # Launch ...
    if __name__ == '__main__':
        app = App[]
    
    5import4= import6import7import8

    import9= tkinter as tk1

    tkinter as tk2tkinter as tk3= tkinter as tk5tkinter as tk6

    tkinter as tk2tkinter as tk8= frame 0]

    frame 2

    frame 3= frame 5

    frame 6frame 7= frame 9=0

    frame 6=2= =4

    =5

    =6= =8= tk.Tk[]0

    tk.Tk[]1

    tk.Tk[]2

    Output:

    Ảnh chụp nhanh sau khi nhấn nút in cho mã trên

    Làm thế nào để bạn nhập một hộp văn bản trong Python?

    Các bước để tạo một hộp nhập bằng Tkinter..
    Bước 1: Tạo khung vẽ. Canvas là màn hình của bạn, nơi bạn có thể đặt các mục, chẳng hạn như hộp nhập, nút, biểu đồ và nhiều hơn nữa. ....
    Bước 2: Thêm hộp nhập. ....
    Bước 3: Bao gồm một chức năng. ....
    Bước 4: Thêm nút. ....
    Bước 5: Chạy mã hoàn chỉnh trong Python ..

    Đầu vào hộp văn bản là gì?

    Hộp văn bản HTML là một khu vực trên màn hình trong đó người dùng có thể nhập đầu vào văn bản. Đây là một yếu tố đầu vào phổ biến được tìm thấy trong nhiều chương trình phần mềm, chẳng hạn như trình duyệt web, ứng dụng email và trình xử lý văn bản. Khi bạn nhấp vào hộp văn bản, con trỏ được bật, cho biết người dùng có thể bắt đầu nhập.an area on the screen wherein the user can enter the text input. It is a common input element found in many software programs, such as web browsers, email clients, and word processors. When you click on the text box, a cursor is enabled, indicating the user can begin typing.

    Làm thế nào để bạn làm một hộp trong Python?

    Làm như vậy:..
    Kéo và thả Audio> Voice> Say Say vào sơ đồ ..
    Nhấp đúp vào nó để hiển thị sơ đồ bên trong ..
    Sao chép hộp văn bản nói ..
    Nhấp vào nhãn gốc để quay lại hành vi ..
    Dán hộp văn bản nói ..
    Xóa hộp nói ..

    Làm thế nào để bạn tạo một khu vực văn bản trong Python?

    Chúng tôi tạo một tiện ích văn bản bằng cách sử dụng phương thức văn bản [].Chúng tôi đặt chiều cao thành 2, tức là hai dòng và chiều rộng thành 30, tức là 30 ký tự.Chúng ta có thể áp dụng phương thức chèn [] trên đối tượng t, mà phương thức văn bản [] đã trả về, để bao gồm văn bản.Chúng tôi thêm hai dòng văn bản.by using the Text[] method. We set the height to 2, i.e. two lines and the width to 30, i.e. 30 characters. We can apply the method insert[] on the object T, which the Text[] method had returned, to include text. We add two lines of text.

    Bài Viết Liên Quan

    Chủ Đề