2015-01-10 1 views
0

Tkinter GUI로 작은 프로그램을 작성하여 2 줄의 텍스트를 입력하고 문자 화면에 표시하도록했습니다. 적용 텍스트를 누르기 전까지는 모든 것이 꽤 멋지게 작동합니다. 그 이유는 LCD의 두 줄에 이상한 값이 나타나는 것 같기 때문입니다.
예 :
구인 라인 1 : "Test"
구인 라인 2 : "Please work"Python 프로그램이 내 16x2 문자 LCD에 이상한 값을 출력하고 있습니다

실제 결과

행 1 : .3047332040L.304
2 호선 :

: 이것은 내 코드 7332320L

입니다

__author__ = 'David' 
from Tkinter import * 
from Adafruit_CharLCD import Adafruit_CharLCD 
from time import sleep 
import psutil 
chargui = Tk() 
lcd = Adafruit_CharLCD() 
lcd.begin(16, 1) 


class FrameWork: 
    def __init__(self, master): 
     frame = Frame(master) 
     frame.pack() 


     # Creation 
     self.lbl_enter1 = Label(frame, text="Enter the first line:") 
     self.lbl_enter2 = Label(frame, text="Enter the second line:") 

     self.ent_line1 = Entry(frame) 
     self.ent_line2 = Entry(frame) 

     self.btn_apply = Button(frame, text="Apply Text", command=self.applymessage) 
     self.btn_cpum = Button(frame, text="CPUMem", command=self.CPUMem) 
     self.btn_quit = Button(frame, text="Quit", command=frame.master.destroy) 


     # Griding 
     self.lbl_enter1.grid(row=0, column=0, sticky=E, padx=2) 
     self.lbl_enter2.grid(row=1, column=0, sticky=E, padx=2) 

     self.ent_line1.grid(row=0, column=1, sticky=W) 
     self.ent_line2.grid(row=1, column=1, sticky=W) 

     self.btn_apply.grid(row=2, column=1, sticky=W, padx=24) 
     self.btn_cpum.grid(row=2, column=0, columnspan=2, sticky=W, padx=85) 
     self.btn_quit.grid(row=2, column=1, sticky=E) 

    def applymessage(self): 
     lcd.clear() 
     lcd.message(str(self.ent_line1)) 
     lcd.message(str(self.ent_line2)) 


    def CPUMem(self): 
     while 1: 
      lcd.clear() 
      lcd.message("CPU: " + str(psutil.cpu_percent()) + "%\n") 
      lcd.message("MEM: " + str(psutil.virtual_memory().percent) + "%") 
      sleep(1) 


g = FrameWork(chargui) 
chargui.mainloop() 

CPUMem 기능. 이 함수는 잘 작동합니다. 그냥 문제가되는 applymessage(self):입니다. 나는 전혀 오류가 발생하지 않습니다. 그래도 lcd.message 함수에서 srt()을 제거하면 문자열에 int를 연결할 수 없다는 메시지가 나타납니다. 모든 솔루션?

편집 : (? 그들은 메모리 위치입니다 추측) 난 그냥 LCD에 그것을 넣는 대신 콘솔에 값을 인쇄했는데, 그것은 여전히 ​​나를 이상한 값을 제공
두 라인
1 호선 : .3047815368L.3047815608L
2 호선 : .3047815368L.3047815648L

답변

2

발견 한대로 LCD는 아무 관련이 없습니다.문제는 str으로 Tkinter를 Entry 개체를 변환하려고 :

str(self.ent_line1) 

은 (print 않습니다 같은) special method, self.ent_line1.__str__()이 객체의 문자열 표현을 얻기 위해 호출합니다. __str__이 유용한 것을하기 위해 정의되었다는 기대는 없습니다.

사실, 대화식 셸을 사용하여 조사하면이 특수 메서드가 부모 클래스에 정의되어 있고 docstring이 "이 위젯의 ​​창 경로 이름 반환"이라는 것을 알 수 있습니다. 그것이 당신이보고있는 것입니다.

는 당신이 실제로 원하는 것은, 문자열로 위젯에 입력 한 텍스트는 get() 주어진다 :

print self.ent_line1.get() 
+0

건배! 이것은 완벽하게 잘 작동 :) – davidpox

1

편집 : 흠, 당신이 다른 대답에 따라이 질문에 전혀 변수에 몇 위젯에 필요하지 않은 것으로 보인다. 내가 TK를 사용했을 때만해도 항상 그랬지만, 위젯에서 직접 get()을 호출하는 것이이 경우 더 쉽습니다. 나는이 배경 지식을 얻기 위해이 대답을 남겨두고있다.

나는 tkinter의 전문가가 아니지만 좀 더 지식있는 대답이 없으면 최선을 다할 것입니다.

문제는 위젯 객체를 직접 인쇄하려고하지만 API가 그렇게 작동하지 않는다는 것입니다. documentation for the Entry widget을 읽으면 StringVar 인스턴스를 해당 인스턴스와 연결해야한다는 것을 알 수 있습니다. This page에는 몇 가지 세부 정보가 있으며 section in the Python docs도 있습니다. 당신의 Entry wigets 구축 할 때

그래서, 당신은 이런 일을 수행해야합니다 : 다음

self.ent_line1_text = StringVar() 
self.ent_line2_text = StringVar() 
self.ent_line1 = Entry(frame, textvariable=self.ent_line1_text) 
self.ent_line2 = Entry(frame, textvariable=self.ent_line2_text) 

그리고 당신의 applymessage()과 같이 보일 것이다 :

def applymessage(self): 
    lcd.clear() 
    lcd.message(self.ent_line1.get()) 
    lcd.message(self.ent_line2.get()) 

뿐만 아니라 get() 방법을 입력 상자의 현재 내용을 검색하려면 프로그래밍 방식으로 변경해야하는 경우 (예 : 일부 기본 텍스트로 텍스트 상자를 초기화하는 경우) set()이 있습니다.

+0

이 도움을 주셔서 감사합니다! – davidpox

+0

맞습니다.'StringVar'는 선택 사항이며, 대부분의 경우 완전히 불필요합니다. –

관련 문제