[참고]


import tkinter as tk

from tkinter import ttk


#create instance

win = tk.Tk()


#add a title

win.title("Python GUI")


tabControl = ttk.Notebook(win)  #create tab control


tab1 = ttk.Frame(tabControl)             #create a tab

tabControl.add(tab1, text = 'Tab 1')     #add the tab

tab2 = ttk.Frame(tabControl)             #add a second tab

tabControl.add(tab2, text = 'Tab 2')     #make second tab visible


tabControl.pack(expand = 1, fill = "both") #pack to make visible


#LabelFrame using tab1 as the parent

mighty = ttk.LabelFrame(tab1, text = 'Mighty Python')

mighty.grid(column = 0, row = 0, padx = 8, pady = 4)


#Label using mighty as the parent

a_label = ttk.Label(mighty, text = "Enter a name:")

a_label.grid(column = 0, row = 0, stick = 'W')


#start GUI

win.mainloop()



[결과]



[참고]

import tkinter as tk

from tkinter import ttk


#create instance

win = tk.Tk()


#add a title

win.title("Python GUI")


#create tab control

tabControl = ttk.Notebook(win)


tab1 = ttk.Frame(tabControl)             #create a tab

tabControl.add(tab1, text = 'Tab 1')     #add the tab

tab2 = ttk.Frame(tabControl)             #add a second tab

tabControl.add(tab2, text = 'Tab 2')     #add second tab


tabControl.pack(expand = 1, fill = "both") #pack to make visible


#start GUI

win.mainloop()


[결과]



[참고]

import tkinter as tk
from tkinter import ttk

win = tk.Tk()                                    # Create instance
win.title("Python GUI")                       # Add a title       
tabControl = ttk.Notebook(win)           # Create Tab Control
tab1 = ttk.Frame(tabControl)               # Create a tab 
tabControl.add(tab1, text='Tab 1')        # Add the tab
tabControl.pack(expand=1, fill="both")  # Pack to make visible

#======================
# Start GUI
#======================
win.mainloop()


[결과]


[참고]

[참고]


1. $sudo nano /etc/nanorc    (nano 에디터로 환경설정 열기, vim을 이용할 때도 관리자권한으로 열어주기)

2. 단축키 ctrl + w 을 눌러서 'tabsize' 찾기

3. 앞에 '#'을 삭제하고 초기 8로 되어 있는 Tapsize를 4로 변경

4. set tabstospaces(tab을 공간으로 전환하는 기능)도 '#'을 삭제하고 설정

5. ctrl + o 로 저장

6. ctrl+ x 로 종료 

7. nano 에디터로 확인해 보기


[참고]


#GUI_radiobutton_widget.py

import tkinter as tk
from tkinter import ttk

#create instance
win = tk.Tk()

#add a title
win.title("Pyton GUI")

#Modify adding a Label
a_label = ttk.Label(win, text = "A Label")
a_label.grid(column = 0, row = 0)

#Modified Button Click Function
def click_me():
    action.configure(text = 'Hello ' + name.get() + ' ' + number_chosen.get())

#Changing our Label
ttk.Label(win, text = "Enter a name: ").grid(column = 0, row = 0)

#Adding a Textbox Entry widget
name = tk.StringVar()
name_entered = ttk.Entry(win, width = 12, textvariable = name)
name_entered.grid(column = 0, row = 1)

#Adding a Button
action = ttk.Button(win, text = "Click Me!", command = click_me)
action.grid(column = 2, row = 1)

#Creating three checkbuttons
ttk.Label(win, text = "Choose a number: ").grid(column = 1, row = 0)
number = tk.StringVar()
number_chosen = ttk.Combobox(win, width = 12, textvariable = number, state = 'readonly')
number_chosen['values'] = (1,2,4,42,100)
number_chosen.grid(column = 1, row = 1)
number_chosen.current(0)

chVarDis = tk.IntVar()
check1 = tk.Checkbutton(win, text = "Disabled", variable = chVarDis, state = 'disabled')
check1.select()
check1.grid(column = 0, row = 4, sticky = tk.W)

chVarUn = tk.IntVar()
check2 = tk.Checkbutton(win, text = "UnChecked", variable = chVarUn)
check2.deselect()
check2.grid(column = 1, row = 4, sticky = tk.W)

chVarEn = tk.IntVar()
check3 = tk.Checkbutton(win, text = "Enabled", variable = chVarEn)
check3.deselect()
check3.grid(column = 2, row = 4, sticky = tk.W)

#GUI Callback function
def checkCallback(*ignoredArgs):
    #only enable one checkbutton
    if chVarUn.get() :
        check3.configure(state = 'disabled')
    else:
        check3.configure(state = 'normal')
    if chVarEn.get():
        check2.configure(state = 'disabled')
    else:
        check2.configure(state = 'normal')

#trace the state of the two checkbuttons
chVarUn.trace('w', lambda unused0, unused1, unused2 : checkCallback())
chVarEn.trace('w', lambda unused0, unused1, unused2 : checkCallback())

#Radiobutton Globals
COLOR1 = "Blue"
COLOR2 = "Gold"
COLOR3 = "Red"
COLOR4 = "Black"

#Radiobutton Callback
def radCall():
    radSel = radVar.get()
    if radSel == 1:
        win.configure(background = COLOR1)
    elif radSel == 2:
        win.configure(background = COLOR2)
    elif radSel == 3:
        win.configure(background = COLOR3)
    elif radSel == 4:
        win.configure(background = COLOR4)

#create three Radiobuttons using one variable
radVar = tk.IntVar()

rad1 = tk.Radiobutton(win, text = COLOR1, variable = radVar, value = 1, command = radCall)
rad1.grid(column = 0, row = 5, sticky = tk.W, columnspan = 3)

rad2 = tk.Radiobutton(win, text = COLOR2, variable = radVar, value = 2, command= radCall)
rad2.grid(column = 1, row = 5, sticky = tk.W, columnspan = 3)

rad3 = tk.Radiobutton(win, text = COLOR3, variable = radVar, value = 3, command = radCall)
rad3.grid(column = 2, row = 5, sticky = tk.W, columnspan = 3)

rad4 = tk.Radiobutton(win, text = COLOR4, variable = radVar, value = 4, command = radCall)
rad4.grid(column = 3, row = 5, sticky = tk.W, columnspan = 3)

name_entered.focus()

#start GUI
win.mainloop()

[결과]


'#Python GUI Programming Cookbook' 카테고리의 다른 글

#Tab 안에 LabelFrame과 Label 넣기  (0) 2018.12.21
#Tab 2개 만들기  (0) 2018.12.21
#Tab 만들기  (0) 2018.12.21
#콤보박스 위젯 만들기  (0) 2018.12.07
#tkinter #메시지 박스 만들기  (0) 2018.12.06

*우분투에서 파이썬 3을 삭제하면 부팅이 안 됨!


[현상]

  • 우분투16.04에 파이썬3.7을 설치했는데 pip3로 설치한 모듈이 import 되지 않는 현상
  • (기존에 설치되어 있는) 파이썬 3.5 버전을 삭제해보기로 함
[실행]
$whereis python    #현재 PC에 설치 되어있는 python 경로를 보여줌
$ whereis python
python: /usr/bin/python /usr/bin/python3.5 /usr/bin/python3.5m /usr/bin/python2.7 /usr/bin/python3.5-config /usr/bin/python3.5m-config /usr/lib/python3.5 /usr/lib/python2.7 /etc/python /etc/python3.5 /etc/python2.7 /usr/local/bin/python3.7m-config /usr/local/bin/python3.7 /usr/local/bin/python3.7m /usr/local/bin/python3.7-config /usr/local/lib/python3.7 /usr/local/lib/python3.5 /usr/local/lib/python2.7 /usr/include/python3.5 /usr/include/python3.5m /usr/share/python /usr/share/man/man1/python.1.gz

$pip3 -V        #파이썬3 모듈 설치할 때 유용한 pip3버전을 확인함


$ pip3 -V
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)


$sudo apt autoremove python3.5    #python3.5 제거



[결과]

  • 부팅 시 작업표시줄 UI등이 나타지 않는 오류 발생
  • 리눅스 시스템 일부가 내장되어 있는 파이썬3.5로 제작되어 있는 느낌임. 우분투에 파이썬3.7을 설치 했음에도 파이썬3.5를 위처럼 제거 했더니 시스템의 오류 발생
  • 라즈베리파이와 동일한 현상임
  • 결론 : 추천하지 않음. 우분투 재설치 예정


[참고]


#======================

# imports

#======================

import tkinter as tk

from tkinter import ttk


# Create instance

win = tk.Tk()   


# Add a title       

win.title("Python GUI")  


# Modify adding a Label

a_label = ttk.Label(win, text="A Label")

a_label.grid(column=0, row=0)


# Modified Button Click Function

def click_me(): 

    action.configure(text='Hello ' + name.get())

#     print(number)

#     print(number.get())


# Changing our Label

ttk.Label(win, text="Enter a name:").grid(column=0, row=0)


# Adding a Textbox Entry widget

name = tk.StringVar()

name_entered = ttk.Entry(win, width=12, textvariable=name)

name_entered.grid(column=0, row=1)                          # column 0


# Adding a Button

action = ttk.Button(win, text="Click Me!", command=click_me)   

action.grid(column=2, row=1)                                # <= change column to 2


ttk.Label(win, text="Choose a number:").grid(column=1, row=0)

number = tk.StringVar()

number_chosen = ttk.Combobox(win, width=12, textvariable=number)

number_chosen['values'] = (1, 2, 4, 42, 100)

number_chosen.grid(column=1, row=1)                         # <= Combobox in column 1

number_chosen.current(0)


name_entered.focus()      # Place cursor into name Entry

#======================

# Start GUI

#======================

win.mainloop()



[결과]

(가운데 숫자1 있는 박스가 콤보 박스)



+ Recent posts