Bài giảng Kỹ thuật lập trình CKT - Chương 8: Lập trình giao diện người dùng trong Python (Graphical User Interface - GUI) - Nguyễn Thanh Nhã
8.2. Minh họa cách tạo GUI đơn giản
Ví dụ 2: Message Widget
• The widget can be used to display short text messages.
• The message widget is similar in its functionality to the Label widget, but it is
more flexible in displaying text, e.g. the font can be changed while the Label
widget can only display text in a single font.
Ví dụ 2: Message Widget
• The widget can be used to display short text messages.
• The message widget is similar in its functionality to the Label widget, but it is
more flexible in displaying text, e.g. the font can be changed while the Label
widget can only display text in a single font.
Bạn đang xem tài liệu "Bài giảng Kỹ thuật lập trình CKT - Chương 8: Lập trình giao diện người dùng trong Python (Graphical User Interface - GUI) - Nguyễn Thanh Nhã", để tải tài liệu gốc về máy hãy click vào nút Download ở trên.
File đính kèm:
- bai_giang_ky_thuat_lap_trinh_ckt_chuong_8_lap_trinh_giao_die.pdf
Nội dung text: Bài giảng Kỹ thuật lập trình CKT - Chương 8: Lập trình giao diện người dùng trong Python (Graphical User Interface - GUI) - Nguyễn Thanh Nhã
- TS. Nguyễn Thanh Nhã 10/29/2019 Chương 8 Lập trình giao diện người dùng trong Python (Graphical User Interface - GUI) Department of Engineering Mechanics – HCMUT 2017 3 Chương 8. Graphical User Interface – GUI PROGRAMMING FOR ENGINEERINGS 8.1. Giới thiệu - Python cung cấp nhiều tùy chọn cho việc phát triển giao diện người dùng (GUIs). Một số công cụ điển hình như: • Tkinter: the Python interface to the Tk GUI toolkit shipped with Python. • wxPython − This is an open-source Python interface for wxWindows • JPython − JPython is a Python port for Java which gives Python scripts seamless access to Java class libraries on the local machine Department of Engineering Mechanics – HCMUT 2019 4 nhanguyen@hcmut.edu.vn 2
- TS. Nguyễn Thanh Nhã 10/29/2019 Chương 8. Graphical User Interface – GUI PROGRAMMING FOR ENGINEERINGS 8.2. Minh họa cách tạo GUI đơn giản Ví dụ 1c: import tkinter as tk root = tk.Tk() logo = tk.PhotoImage(file="python_logo.gif") explanation = """At present, only GIF and PPM/PGM formats are supported, but an interface exists to allow additional image file formats to be added easily.""" w = tk.Label(root, compound = tk.CENTER, text=explanation, image=logo).pack(side="right") root.mainloop() Department of Engineering Mechanics – HCMUT 2019 7 Chương 8. Graphical User Interface – GUI PROGRAMMING FOR ENGINEERINGS 8.2. Minh họa cách tạo GUI đơn giản Ví dụ 2: Message Widget • The widget can be used to display short text messages. • The message widget is similar in its functionality to the Label widget, butitis more flexible in displaying text, e.g. the font can be changed while the Label widget can only display text in a single font. import tkinter as tk master = tk.Tk() whatever_you_do = "Whatever you do will be insignificant, but it is very important that you do it.\n(Mahatma Gandhi)" msg = tk.Message(master, text = whatever_you_do) msg.config(bg='lightgreen', font=('times', 24, 'italic')) msg.pack() tk.mainloop() Department of Engineering Mechanics – HCMUT 2019 8 nhanguyen@hcmut.edu.vn 4
- TS. Nguyễn Thanh Nhã 10/29/2019 Chương 8. Graphical User Interface – GUI PROGRAMMING FOR ENGINEERINGS 8.2. Minh họa cách tạo GUI đơn giản Ví dụ 4a: Check box from tkinter import * master = Tk() var1 = IntVar() Checkbutton(master, text="male", variable=var1).grid(row=0, sticky=W) var2 = IntVar() Checkbutton(master, text="female", variable=var2).grid(row=1, sticky=W) mainloop() Department of Engineering Mechanics – HCMUT 2019 11 Chương 8. Graphical User Interface – GUI PROGRAMMING FOR ENGINEERINGS 8.2. Minh họa cách tạo GUI đơn giản Ví dụ 4b: Check box from tkinter import * master = Tk() def var_states(): print("male: %d,\nfemale: %d" % (var1.get(), var2.get())) Label(master, text="Your sex:").grid(row=0, sticky=W) var1 = IntVar() Checkbutton(master, text="male", variable=var1).grid(row=1, sticky=W) var2 = IntVar() Checkbutton(master, text="female", variable=var2).grid(row=2, sticky=W) Button(master, text='Quit', command=master.quit).grid(row=3, sticky=W, pady=4) Button(master, text='Show', command=var_states).grid(row=4, sticky=W, pady=4) mainloop() Department of Engineering Mechanics – HCMUT 2019 12 nhanguyen@hcmut.edu.vn 6
- TS. Nguyễn Thanh Nhã 10/29/2019 Chương 8. Graphical User Interface – GUI PROGRAMMING FOR ENGINEERINGS 8.2. Minh họa cách tạo GUI đơn giản Ví dụ 5a: Radio Button import tkinter as tk root = tk.Tk() v = tk.IntVar() tk.Label(root, text="""Choose a programming language:""", justify = tk.LEFT, padx = 20).pack() tk.Radiobutton(root, text="Python", padx = 20, variable=v, value=1).pack(anchor=tk.W) tk.Radiobutton(root, text="Perl", padx = 20, variable=v, value=2).pack(anchor=tk.W) root.mainloop() Department of Engineering Mechanics – HCMUT 2019 15 Chương 8. Graphical User Interface – GUI PROGRAMMING FOR ENGINEERINGS 8.2. Minh họa cách tạo GUI đơn giản Ví dụ 5b: Radio Button import tkinter as tk root = tk.Tk() v = tk.IntVar() v.set(1) # initializing the choice, i.e. Python languages = [("Python",1),("Perl",2),("Java",3),("C++",4),("C",5)] def ShowChoice(): print(v.get()) tk.Label(root, text="""Choose your favourite programming language:""", justify = tk.LEFT, padx = 20).pack() for val, language in enumerate(languages): tk.Radiobutton(root, text=language, padx = 20, variable=v, command=ShowChoice, value=val).pack(anchor=tk.W) root.mainloop() Department of Engineering Mechanics – HCMUT 2019 16 nhanguyen@hcmut.edu.vn 8
- TS. Nguyễn Thanh Nhã 10/29/2019 Chương 8. Graphical User Interface – GUI PROGRAMMING FOR ENGINEERINGS ‐index.htm#introduction Department of Engineering Mechanics – HCMUT 2019 19 Chương 8. Graphical User Interface – GUI PROGRAMMING FOR ENGINEERINGS 8.2. Minh họa cách tạo GUI đơn giản Ví dụ 3: from tkinter import * class SampleApp(Tk): Entry def __init__(self): Tk.__init__(self) self.var = StringVar() self.entry1 = Entry(self) self.entry2 = Entry(self) self.entry3 = Entry(self, textvariable=self.var) self.button = Button(self, text="=", command=self.on_button) self.label = Label(self, text = "+") self.entry1.pack() self.label.pack() self.entry2.pack() self.button.pack() self.entry3.pack() def on_button(self): print(self.entry1.get()) print(self.entry2.get()) ss = float(self.entry1.get()) + float(self.entry2.get()) self.var.set(ss) print(self.entry3.get()) app = SampleApp() app.mainloop() Department of Engineering Mechanics – HCMUT 2019 20 nhanguyen@hcmut.edu.vn 10